/** * Creates an instance of AppServiceManager that exposes Web resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription UUID * @return the AppServiceManager */ public static AppServiceManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new AppServiceManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(), subscriptionId); } /**
if (!response.isSuccessful()) { String content = errorBody(response.body()); AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter(); CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class); if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) { Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(chain.request().url().toString()); matcher.find(); RestClient restClient = new RestClient.Builder() .withBaseUrl("https://" + chain.request().url().host()) .withCredentials(credentials) .withSerializerAdapter(jacksonAdapter) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(); ResourceManager resourceManager = ResourceManager.authenticate(restClient)
/** * Creates an instance of KeyVaultManager that exposes KeyVault resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription UUID * @return the KeyVaultManager */ public static KeyVaultManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new KeyVaultManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withInterceptor(new ProviderRegistrationInterceptor(credentials)) .withInterceptor(new ResourceManagerThrottlingInterceptor()) .build(), credentials.domain(), subscriptionId); }
/** * Creates an MSI credential for app services. * @param environment the environment this application is running in * @param endpoint the MSI endpoint * @param secret the MSI secret */ public AppServiceMSICredentials(AzureEnvironment environment, String endpoint, String secret) { super(environment, null); if (endpoint == null) { throw new IllegalArgumentException("endpoint == null"); } if (secret == null) { throw new IllegalArgumentException("secret == null"); } this.endpoint = endpoint; this.secret = secret; this.adapter = new AzureJacksonAdapter(); }
@Override public Response intercept(Chain chain) throws IOException { if ("PUT".equals(chain.request().method()) && chain.request().url().uri().toString().contains("/resourcegroups/") && LOGGING_CONTEXT.equals(chain.request().header("x-ms-logging-context"))) { String body = bodyToString(chain.request()); ResourceGroupInner rg = adapter.deserialize(body, ResourceGroupInner.class); if (rg == null) { throw new RuntimeException("Failed to deserialize " + body); } Map<String, String> tags = rg.tags(); if (tags == null) { tags = new HashMap<>(); } tags.put("product", "javasdk"); tags.put("cause", "automation"); tags.put("date", DateTime.now(DateTimeZone.UTC).toString()); if (System.getenv("ENV_JOB_NAME") != null) { tags.put("job", System.getenv("ENV_JOB_NAME")); } rg.withTags(tags); String newBody = adapter.serialize(rg); Request newRequest = chain.request().newBuilder() .put(RequestBody.create(chain.request().body().contentType(), newBody)) .header("Content-Length", String.valueOf(newBody.length())).build(); return chain.proceed(newRequest); } return chain.proceed(chain.request()); }
String result = reader.readLine(); MSIToken msiToken = adapter.deserialize(result, MSIToken.class); return msiToken.accessToken(); } catch (Exception e) {
/** * Authenticate to Azure using an Azure credentials object. * * @param credentials the credentials object * @return the authenticated Azure client */ public static Authenticated authenticate(AzureTokenCredentials credentials) { return new AuthenticatedImpl(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withInterceptor(new ProviderRegistrationInterceptor(credentials)) .withInterceptor(new ResourceManagerThrottlingInterceptor()) .build(), credentials.domain()); }
protected AzureConfigurableImpl() { this.restClientBuilder = new RestClient.Builder() .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()); }
String result = reader.readLine(); MSIToken msiToken = adapter.deserialize(result, MSIToken.class); return msiToken.accessToken(); } catch (Exception e) {
/** * Creates an instance of ResourcesManager that exposes Resources resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription UUID * @return the ResourcesManager */ public static ResourcesManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new ResourcesManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(), subscriptionId); } /**
/** * Authenticate to Azure using an Azure credentials object. * * @param credentials the credentials object * @return the authenticated Azure client */ public static Authenticated authenticate(AzureTokenCredentials credentials) { return new AuthenticatedImpl(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withInterceptor(new ProviderRegistrationInterceptor(credentials)) .withInterceptor(new ResourceManagerThrottlingInterceptor()) .build(), credentials.domain()); }
protected AzureConfigurableImpl() { this.restClientBuilder = new RestClient.Builder() .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()); }
if (!response.isSuccessful()) { String content = errorBody(response.body()); AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter(); CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class); if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) { Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(chain.request().url().toString()); matcher.find(); RestClient restClient = new RestClient.Builder() .withBaseUrl("https://" + chain.request().url().host()) .withCredentials(credentials) .withSerializerAdapter(jacksonAdapter) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(); ResourceManager resourceManager = ResourceManager.authenticate(restClient)
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 100); String result = reader.readLine(); return adapter.deserialize(result, MSIToken.class); } catch (Exception exception) { int responseCode = connection.getResponseCode();
/** * Creates an instance of AuthorizationManager that exposes Authorization resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription UUID * @return the AuthorizationManager */ public static AuthorizationManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new AuthorizationManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(), subscriptionId); } /**
/** * Creates an instance of SqlServer that exposes Compute resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription * @return the SqlServer */ public static SqlServerManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new SqlServerManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withInterceptor(new ProviderRegistrationInterceptor(credentials)) .withInterceptor(new ResourceManagerThrottlingInterceptor()) .build(), credentials.domain(), subscriptionId); }
protected AzureConfigurableCoreImpl() { this.restClientBuilder = new RestClient.Builder() .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()); }
/** * Creates an instance of LocksManager that exposes Authorization resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription UUID * @return the LocksManager */ public static LocksManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new LocksManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .build(), subscriptionId); } /**
/** * Creates an instance of SqlServer that exposes Compute resource management API entry points. * * @param credentials the credentials to use * @param subscriptionId the subscription * @return the SqlServer */ public static SqlServerManager authenticate(AzureTokenCredentials credentials, String subscriptionId) { return new SqlServerManager(new RestClient.Builder() .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withInterceptor(new ProviderRegistrationInterceptor(credentials)) .withInterceptor(new ResourceManagerThrottlingInterceptor()) .build(), credentials.domain(), subscriptionId); }
public void addKeyVaultPropertySource() { final String vaultUri = getProperty(this.environment, Constants.AZURE_KEYVAULT_VAULT_URI); final Long refreshInterval = Optional.ofNullable( this.environment.getProperty(Constants.AZURE_KEYVAULT_REFRESH_INTERVAL)) .map(Long::valueOf).orElse(Constants.DEFAULT_REFRESH_INTERVAL_MS); final ServiceClientCredentials credentials = getCredentials(); final RestClient restClient = new RestClient.Builder().withBaseUrl(vaultUri) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withUserAgent(UserAgent.getUserAgent(Constants.AZURE_KEYVAULT_USER_AGENT, allowTelemetry(this.environment))) .build(); final KeyVaultClient kvClient = new KeyVaultClient(restClient); this.trackCustomEvent(); try { final MutablePropertySources sources = this.environment.getPropertySources(); final KeyVaultOperation kvOperation = new KeyVaultOperation(kvClient, vaultUri, refreshInterval); if (sources.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { sources.addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new KeyVaultPropertySource(kvOperation)); } else { sources.addFirst(new KeyVaultPropertySource(kvOperation)); } } catch (final Exception ex) { throw new IllegalStateException("Failed to configure KeyVault property source", ex); } }