Search in sources :

Example 11 with IamCredentialsClient

use of com.google.cloud.iam.credentials.v1.IamCredentialsClient in project workbench by all-of-us.

the class GenerateImpersonatedUserTokens method writeTokens.

private void writeTokens(String projectId, String[] usernames, String[] filenames) throws IOException {
    final String saEmail = ServiceAccounts.getServiceAccountEmail(ADMIN_SERVICE_ACCOUNT_NAME, projectId);
    final IamCredentialsClient credsClient = IamCredentialsClient.create();
    final HttpTransport transport = new ApacheHttpTransport();
    final Gson gson = new Gson();
    for (int i = 0; i < usernames.length; i++) {
        final String username = usernames[i];
        final String filename = filenames[i];
        log.info(String.format("Writing impersonated user credential for %s to %s", username, filename));
        final DelegatedUserCredentials creds = new DelegatedUserCredentials(saEmail, username, FireCloudConfig.BILLING_SCOPES, credsClient, transport);
        creds.refresh();
        final String token = creds.getAccessToken().getTokenValue();
        try (FileWriter w = new FileWriter(filename)) {
            w.write(gson.toJson(ImmutableMap.of("created_at_epoch_seconds", Instant.now().getEpochSecond(), "token", token)));
        }
    }
}
Also used : ApacheHttpTransport(com.google.api.client.http.apache.ApacheHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) DelegatedUserCredentials(org.pmiops.workbench.auth.DelegatedUserCredentials) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient) ApacheHttpTransport(com.google.api.client.http.apache.ApacheHttpTransport)

Example 12 with IamCredentialsClient

use of com.google.cloud.iam.credentials.v1.IamCredentialsClient in project workbench by all-of-us.

the class ImpersonatedServiceAccountApiClientFactory method getAccessToken.

public static String getAccessToken(String targetServiceAccount) throws IOException {
    IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create();
    String accessToken = iamCredentialsClient.generateAccessToken("projects/-/serviceAccounts/" + targetServiceAccount, Collections.EMPTY_LIST, Arrays.asList(FC_SCOPES), Duration.newBuilder().setSeconds(60 * 60).build()).getAccessToken();
    return accessToken;
}
Also used : IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient)

Example 13 with IamCredentialsClient

use of com.google.cloud.iam.credentials.v1.IamCredentialsClient in project gapic-generator-java by googleapis.

the class SyncSignJwt method syncSignJwt.

public static void syncSignJwt() throws Exception {
    // It may require modifications to work in your environment.
    try (IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create()) {
        SignJwtRequest request = SignJwtRequest.newBuilder().setName(ServiceAccountName.of("[PROJECT]", "[SERVICE_ACCOUNT]").toString()).addAllDelegates(new ArrayList<String>()).setPayload("payload-786701938").build();
        SignJwtResponse response = iamCredentialsClient.signJwt(request);
    }
}
Also used : SignJwtResponse(com.google.cloud.iam.credentials.v1.SignJwtResponse) IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient) SignJwtRequest(com.google.cloud.iam.credentials.v1.SignJwtRequest)

Example 14 with IamCredentialsClient

use of com.google.cloud.iam.credentials.v1.IamCredentialsClient in project gapic-generator-java by googleapis.

the class SyncSignJwtStringListstringString method syncSignJwtStringListstringString.

public static void syncSignJwtStringListstringString() throws Exception {
    // It may require modifications to work in your environment.
    try (IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create()) {
        String name = ServiceAccountName.of("[PROJECT]", "[SERVICE_ACCOUNT]").toString();
        List<String> delegates = new ArrayList<>();
        String payload = "payload-786701938";
        SignJwtResponse response = iamCredentialsClient.signJwt(name, delegates, payload);
    }
}
Also used : SignJwtResponse(com.google.cloud.iam.credentials.v1.SignJwtResponse) ArrayList(java.util.ArrayList) IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient)

Example 15 with IamCredentialsClient

use of com.google.cloud.iam.credentials.v1.IamCredentialsClient in project gapic-generator-java by googleapis.

the class SyncGenerateAccessTokenStringListstringListstringDuration method syncGenerateAccessTokenStringListstringListstringDuration.

public static void syncGenerateAccessTokenStringListstringListstringDuration() throws Exception {
    // It may require modifications to work in your environment.
    try (IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create()) {
        String name = ServiceAccountName.of("[PROJECT]", "[SERVICE_ACCOUNT]").toString();
        List<String> delegates = new ArrayList<>();
        List<String> scope = new ArrayList<>();
        Duration lifetime = Duration.newBuilder().build();
        GenerateAccessTokenResponse response = iamCredentialsClient.generateAccessToken(name, delegates, scope, lifetime);
    }
}
Also used : ArrayList(java.util.ArrayList) IamCredentialsClient(com.google.cloud.iam.credentials.v1.IamCredentialsClient) Duration(com.google.protobuf.Duration) GenerateAccessTokenResponse(com.google.cloud.iam.credentials.v1.GenerateAccessTokenResponse)

Aggregations

IamCredentialsClient (com.google.cloud.iam.credentials.v1.IamCredentialsClient)21 ArrayList (java.util.ArrayList)11 ServiceAccountName (com.google.cloud.iam.credentials.v1.ServiceAccountName)5 SignJwtResponse (com.google.cloud.iam.credentials.v1.SignJwtResponse)5 GenerateIdTokenResponse (com.google.cloud.iam.credentials.v1.GenerateIdTokenResponse)4 SignBlobResponse (com.google.cloud.iam.credentials.v1.SignBlobResponse)4 ByteString (com.google.protobuf.ByteString)4 GenerateAccessTokenResponse (com.google.cloud.iam.credentials.v1.GenerateAccessTokenResponse)3 IamCredentialsSettings (com.google.cloud.iam.credentials.v1.IamCredentialsSettings)3 HttpTransport (com.google.api.client.http.HttpTransport)2 GenerateIdTokenRequest (com.google.cloud.iam.credentials.v1.GenerateIdTokenRequest)2 SignBlobRequest (com.google.cloud.iam.credentials.v1.SignBlobRequest)2 SignJwtRequest (com.google.cloud.iam.credentials.v1.SignJwtRequest)2 Duration (com.google.protobuf.Duration)2 IOException (java.io.IOException)2 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 ApacheHttpTransport (com.google.api.client.http.apache.ApacheHttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1