use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project jade-data-repo by DataBiosphere.
the class KubernetesClientUtils method buildKubernetesClientObject.
/**
* Build the singleton Kubernetes client objects. This method should be called once at the
* beginning of a test run, and then all subsequent fetches should use the getter methods instead.
*
* @param server the server specification that points to the relevant Kubernetes cluster
*/
public static void buildKubernetesClientObject(ServerSpecification server) throws Exception {
// call the fetchGKECredentials script that uses gcloud to generate the kubeconfig file
List<String> scriptArgs = new ArrayList<>();
scriptArgs.add("tools/fetchGKECredentials.sh");
scriptArgs.add(server.clusterShortName);
scriptArgs.add(server.region);
scriptArgs.add(server.project);
ProcessUtils.executeCommand("sh", scriptArgs);
// path to kubeconfig file, that was just created/updated by gcloud get-credentials above
String kubeConfigPath = System.getProperty("user.home") + "/.kube/config";
// load the kubeconfig object from the file
InputStreamReader filereader = new InputStreamReader(new FileInputStream(kubeConfigPath), StandardCharsets.UTF_8);
KubeConfig kubeConfig = KubeConfig.loadKubeConfig(filereader);
// get a refreshed SA access token and its expiration time
GoogleCredentials applicationDefaultCredentials = AuthenticationUtils.getApplicationDefaultCredential();
AccessToken accessToken = AuthenticationUtils.getAccessToken(applicationDefaultCredentials);
Instant tokenExpiration = accessToken.getExpirationTime().toInstant();
String expiryUTC = tokenExpiration.atZone(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT);
// USERS: build list of one user, the SA
LinkedHashMap<String, Object> authConfigSA = new LinkedHashMap<>();
authConfigSA.put("access-token", accessToken.getTokenValue());
authConfigSA.put("expiry", expiryUTC);
LinkedHashMap<String, Object> authProviderSA = new LinkedHashMap<>();
authProviderSA.put("name", "gcp");
authProviderSA.put("config", authConfigSA);
LinkedHashMap<String, Object> userSA = new LinkedHashMap<>();
userSA.put("auth-provider", authProviderSA);
LinkedHashMap<String, Object> userWrapperSA = new LinkedHashMap<>();
userWrapperSA.put("name", server.clusterName);
userWrapperSA.put("user", userSA);
ArrayList<Object> usersList = new ArrayList<>();
usersList.add(userWrapperSA);
// CONTEXTS: build list of one context, the specified cluster
LinkedHashMap<String, Object> context = new LinkedHashMap<>();
context.put("cluster", server.clusterName);
context.put("user", // when is the user ever different from the cluster name?
server.clusterName);
LinkedHashMap<String, Object> contextWrapper = new LinkedHashMap<>();
contextWrapper.put("name", server.clusterName);
contextWrapper.put("context", context);
ArrayList<Object> contextsList = new ArrayList<>();
contextsList.add(contextWrapper);
// CLUSTERS: use the cluster list read in from the kubeconfig file, because I can't figure out
// how to get the certificate-authority-data and server address for the cluster via the Java
// client library, only with gcloud
ArrayList<Object> clusters = kubeConfig.getClusters();
// build the config object, replacing the contexts and users lists from the kubeconfig file with
// the ones constructed programmatically above
kubeConfig = new KubeConfig(contextsList, clusters, usersList);
kubeConfig.setContext(server.clusterName);
// build the client object from the config
ApiClient client = ClientBuilder.kubeconfig(kubeConfig).build();
// set the global default client to the one created above because the CoreV1Api and AppsV1Api
// constructors get the client object from the global configuration
Configuration.setDefaultApiClient(client);
kubernetesClientCoreObject = new CoreV1Api();
kubernetesClientAppsObject = new AppsV1Api();
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project terra-resource-buffer by DataBiosphere.
the class BufferServiceUtils method getClient.
/**
* Build the Buffer Service API client object for the given server specification.
*
* @param server the server we are testing against
* @return the API client object
*/
public static ApiClient getClient(ServerSpecification server) throws IOException {
if (Strings.isNullOrEmpty(server.bufferUri)) {
throw new IllegalArgumentException("Buffer Service URI cannot be empty");
}
if (server.bufferClientServiceAccount == null) {
throw new IllegalArgumentException("Buffer Service client service account is required");
}
// refresh the client service account token
GoogleCredentials serviceAccountCredential = AuthenticationUtils.getServiceAccountCredential(server.bufferClientServiceAccount, AuthenticationUtils.userLoginScopes);
AccessToken accessToken = AuthenticationUtils.getAccessToken(serviceAccountCredential);
logger.debug("Generated access token for buffer service client SA: {}", server.bufferClientServiceAccount.name);
// build the client object
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(server.bufferUri);
apiClient.setAccessToken(accessToken.getTokenValue());
return apiClient;
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.
the class ArtifactRegistryRequestInitializerTest method testInitialize.
@Test
public void testInitialize() throws Exception {
Credentials creds = GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000))));
ArtifactRegistryRequestInitializer initializer = new ArtifactRegistryRequestInitializer(creds, 100);
MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("test content")).build();
GenericUrl url = new GenericUrl("https://www.example.com");
HttpRequestFactory requestFactory = transport.createRequestFactory(initializer);
HttpRequest request = requestFactory.buildHeadRequest(url);
Assert.assertEquals(request.getReadTimeout(), 100);
Assert.assertEquals(request.getHeaders().getFirstHeaderStringValue("Authorization"), "Bearer test-access-token");
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.
the class ArtifactRegistryWagonTest method testHeadExists.
@Test
public void testHeadExists() throws Exception {
MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse().setStatusCode(HttpStatusCodes.STATUS_CODE_OK)).build();
ArtifactRegistryWagon wagon = new ArtifactRegistryWagon();
wagon.setCredentialProvider(() -> GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000)))));
wagon.setHttpTransportFactory(() -> transport);
wagon.connect(new Repository("my-repo", REPO_URL));
Assert.assertTrue(wagon.resourceExists("my/resource"));
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project artifact-registry-maven-tools by GoogleCloudPlatform.
the class ArtifactRegistryWagonTest method testAuthenticatedPut.
@Test
public void testAuthenticatedPut() throws Exception {
MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpResponse(new MockLowLevelHttpResponse()).build();
ArtifactRegistryWagon wagon = new ArtifactRegistryWagon();
wagon.setCredentialProvider(() -> GoogleCredentials.create(new AccessToken("test-access-token", Date.from(Instant.now().plusSeconds(1000)))));
wagon.setHttpTransportFactory(() -> transport);
wagon.connect(new Repository("my-repo", REPO_URL));
File f = FileTestUtils.createUniqueFile("my/artifact/dir", "test");
Files.asCharSink(f, Charset.defaultCharset()).write("test content");
wagon.put(f, "my/resource");
String authHeader = transport.getLowLevelHttpRequest().getFirstHeaderValue("Authorization");
Assert.assertEquals("Bearer test-access-token", authHeader);
Assert.assertEquals("test content", transport.getLowLevelHttpRequest().getContentAsString());
Assert.assertEquals("https://maven.pkg.dev/my-project/my-repo/my/resource", transport.getLowLevelHttpRequest().getUrl());
}
Aggregations