Search in sources :

Example 1 with SignJwtResponse

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

the class SyncSignJwtServiceaccountnameListstringString method syncSignJwtServiceaccountnameListstringString.

public static void syncSignJwtServiceaccountnameListstringString() throws Exception {
    // It may require modifications to work in your environment.
    try (IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create()) {
        ServiceAccountName name = ServiceAccountName.of("[PROJECT]", "[SERVICE_ACCOUNT]");
        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) ServiceAccountName(com.google.cloud.iam.credentials.v1.ServiceAccountName)

Example 2 with SignJwtResponse

use of com.google.cloud.iam.credentials.v1.SignJwtResponse in project spring-vault by spring-projects.

the class GcpIamCredentialsAuthenticationUnitTests method shouldLogin.

@Test
void shouldLogin() {
    this.serverCall = ((request, responseObserver) -> {
        SignJwtResponse signJwtResponse = SignJwtResponse.newBuilder().setSignedJwt("my-jwt").setKeyId("key-id").build();
        responseObserver.onNext(signJwtResponse);
        responseObserver.onCompleted();
    });
    this.mockRest.expect(requestTo("/auth/gcp/login")).andExpect(method(HttpMethod.POST)).andExpect(jsonPath("$.role").value("dev-role")).andExpect(jsonPath("$.jwt").value("my-jwt")).andRespond(withSuccess().contentType(MediaType.APPLICATION_JSON).body("{" + "\"auth\":{\"client_token\":\"my-token\", \"renewable\": true, \"lease_duration\": 10}" + "}"));
    PrivateKey privateKeyMock = mock(PrivateKey.class);
    ServiceAccountCredentials credential = (ServiceAccountCredentials) ServiceAccountCredentials.newBuilder().setClientEmail("hello@world").setProjectId("foobar").setPrivateKey(privateKeyMock).setPrivateKeyId("key-id").setAccessToken(new AccessToken("foobar", Date.from(Instant.now().plus(1, ChronoUnit.DAYS)))).build();
    GcpIamCredentialsAuthenticationOptions options = GcpIamCredentialsAuthenticationOptions.builder().role("dev-role").credentials(credential).build();
    GcpIamCredentialsAuthentication authentication = new GcpIamCredentialsAuthentication(options, this.restTemplate, FixedTransportChannelProvider.create(GrpcTransportChannel.create(managedChannel)));
    VaultToken login = authentication.login();
    assertThat(login).isInstanceOf(LoginToken.class);
    assertThat(login.getToken()).isEqualTo("my-token");
    LoginToken loginToken = (LoginToken) login;
    assertThat(loginToken.isRenewable()).isTrue();
    assertThat(loginToken.getLeaseDuration()).isEqualTo(Duration.ofSeconds(10));
}
Also used : MockRestResponseCreators.withSuccess(org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess) SignJwtResponse(com.google.cloud.iam.credentials.v1.SignJwtResponse) BeforeEach(org.junit.jupiter.api.BeforeEach) ManagedChannel(io.grpc.ManagedChannel) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProtoLiteUtils(io.grpc.protobuf.lite.ProtoLiteUtils) MockRestRequestMatchers.method(org.springframework.test.web.client.match.MockRestRequestMatchers.method) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) PrefixAwareUriTemplateHandler(org.springframework.vault.client.VaultClients.PrefixAwareUriTemplateHandler) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) ServerCalls.asyncUnaryCall(io.grpc.stub.ServerCalls.asyncUnaryCall) Duration(java.time.Duration) MethodDescriptor(io.grpc.MethodDescriptor) Server(io.grpc.Server) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) MockRestRequestMatchers.jsonPath(org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath) IOException(java.io.IOException) Instant(java.time.Instant) MockRestRequestMatchers.requestTo(org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo) Test(org.junit.jupiter.api.Test) GrpcTransportChannel(com.google.api.gax.grpc.GrpcTransportChannel) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) PrivateKey(java.security.PrivateKey) SignJwtRequest(com.google.cloud.iam.credentials.v1.SignJwtRequest) VaultToken(org.springframework.vault.support.VaultToken) ServerCalls(io.grpc.stub.ServerCalls) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Mockito.mock(org.mockito.Mockito.mock) AccessToken(com.google.auth.oauth2.AccessToken) SignJwtResponse(com.google.cloud.iam.credentials.v1.SignJwtResponse) PrivateKey(java.security.PrivateKey) AccessToken(com.google.auth.oauth2.AccessToken) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) VaultToken(org.springframework.vault.support.VaultToken) Test(org.junit.jupiter.api.Test)

Example 3 with SignJwtResponse

use of com.google.cloud.iam.credentials.v1.SignJwtResponse 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 4 with SignJwtResponse

use of com.google.cloud.iam.credentials.v1.SignJwtResponse 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 5 with SignJwtResponse

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

the class AsyncSignJwt method asyncSignJwt.

public static void asyncSignJwt() 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();
        ApiFuture<SignJwtResponse> future = iamCredentialsClient.signJwtCallable().futureCall(request);
        // Do something.
        SignJwtResponse response = future.get();
    }
}
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)

Aggregations

SignJwtResponse (com.google.cloud.iam.credentials.v1.SignJwtResponse)6 IamCredentialsClient (com.google.cloud.iam.credentials.v1.IamCredentialsClient)5 SignJwtRequest (com.google.cloud.iam.credentials.v1.SignJwtRequest)3 ServiceAccountName (com.google.cloud.iam.credentials.v1.ServiceAccountName)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 GrpcTransportChannel (com.google.api.gax.grpc.GrpcTransportChannel)1 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)1 AccessToken (com.google.auth.oauth2.AccessToken)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 IamCredentialsSettings (com.google.cloud.iam.credentials.v1.IamCredentialsSettings)1 ManagedChannel (io.grpc.ManagedChannel)1 MethodDescriptor (io.grpc.MethodDescriptor)1 Server (io.grpc.Server)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 InProcessChannelBuilder (io.grpc.inprocess.InProcessChannelBuilder)1 InProcessServerBuilder (io.grpc.inprocess.InProcessServerBuilder)1 ProtoLiteUtils (io.grpc.protobuf.lite.ProtoLiteUtils)1 ServerCalls (io.grpc.stub.ServerCalls)1 ServerCalls.asyncUnaryCall (io.grpc.stub.ServerCalls.asyncUnaryCall)1