|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.vault.authentication; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.util.Assert; |
| 25 | +import org.springframework.vault.VaultException; |
| 26 | +import org.springframework.vault.support.VaultToken; |
| 27 | +import org.springframework.web.client.RestOperations; |
| 28 | + |
| 29 | +import com.google.api.client.http.HttpTransport; |
| 30 | +import com.google.api.client.json.JsonFactory; |
| 31 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 32 | +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
| 33 | +import com.google.api.gax.rpc.TransportChannelProvider; |
| 34 | +import com.google.auth.oauth2.GoogleCredentials; |
| 35 | +import com.google.cloud.iam.credentials.v1.IamCredentialsClient; |
| 36 | +import com.google.cloud.iam.credentials.v1.IamCredentialsSettings; |
| 37 | +import com.google.cloud.iam.credentials.v1.ServiceAccountName; |
| 38 | +import com.google.cloud.iam.credentials.v1.SignJwtResponse; |
| 39 | +import com.google.cloud.iam.credentials.v1.stub.IamCredentialsStubSettings; |
| 40 | + |
| 41 | +/** |
| 42 | + * GCP IAM credentials login implementation using GCP IAM service accounts to legitimate |
| 43 | + * its authenticity via JSON Web Token. |
| 44 | + * <p/> |
| 45 | + * This authentication method uses Googles IAM Credentials API to obtain a signed token |
| 46 | + * for a specific {@link com.google.api.client.auth.oauth2.Credential}. Service account |
| 47 | + * details are obtained from a {@link GoogleCredentials} that can be retrieved either from |
| 48 | + * a JSON file or the runtime environment (GAE, GCE). |
| 49 | + * <p/> |
| 50 | + * {@link GcpIamCredentialsAuthentication} uses Google Java API that uses synchronous API. |
| 51 | + * |
| 52 | + * @author Andreas Gebauer |
| 53 | + * @since 2.4 |
| 54 | + * @see GcpIamCredentialsAuthenticationOptions |
| 55 | + * @see HttpTransport |
| 56 | + * @see GoogleCredentials |
| 57 | + * @see GoogleCredentials#getApplicationDefault() |
| 58 | + * @see RestOperations |
| 59 | + * @see <a href="https://www.vaultproject.io/docs/auth/gcp.html">Auth Backend: gcp |
| 60 | + * (IAM)</a> |
| 61 | + * @see <a href= |
| 62 | + * "https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signJwt">GCP: |
| 63 | + * projects.serviceAccounts.signJwt</a> |
| 64 | + */ |
| 65 | +public class GcpIamCredentialsAuthentication extends GcpJwtAuthenticationSupport implements ClientAuthentication { |
| 66 | + |
| 67 | + private static final JsonFactory JSON_FACTORY = new JacksonFactory(); |
| 68 | + |
| 69 | + private final GcpIamCredentialsAuthenticationOptions options; |
| 70 | + |
| 71 | + private final TransportChannelProvider transportChannelProvider; |
| 72 | + |
| 73 | + private final GoogleCredentials credentials; |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a new instance of {@link GcpIamCredentialsAuthentication} given |
| 77 | + * {@link GcpIamCredentialsAuthenticationOptions} and {@link RestOperations}. This |
| 78 | + * constructor initializes {@link InstantiatingGrpcChannelProvider} for Google API |
| 79 | + * usage. |
| 80 | + * @param options must not be {@literal null}. |
| 81 | + * @param restOperations HTTP client for for Vault login, must not be {@literal null}. |
| 82 | + */ |
| 83 | + public GcpIamCredentialsAuthentication(GcpIamCredentialsAuthenticationOptions options, |
| 84 | + RestOperations restOperations) { |
| 85 | + this(options, restOperations, IamCredentialsStubSettings.defaultGrpcTransportProviderBuilder().build()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create a new instance of {@link GcpIamCredentialsAuthentication} given |
| 90 | + * {@link GcpIamCredentialsAuthenticationOptions}, {@link RestOperations} and |
| 91 | + * {@link TransportChannelProvider}. |
| 92 | + * @param options must not be {@literal null}. |
| 93 | + * @param restOperations HTTP client for for Vault login, must not be {@literal null}. |
| 94 | + * @param transportChannelProvider Provider for transport channel Google API use, must |
| 95 | + * not be {@literal null}. |
| 96 | + */ |
| 97 | + public GcpIamCredentialsAuthentication(GcpIamCredentialsAuthenticationOptions options, |
| 98 | + RestOperations restOperations, TransportChannelProvider transportChannelProvider) { |
| 99 | + |
| 100 | + super(restOperations); |
| 101 | + |
| 102 | + Assert.notNull(options, "GcpAuthenticationOptions must not be null"); |
| 103 | + Assert.notNull(restOperations, "RestOperations must not be null"); |
| 104 | + Assert.notNull(transportChannelProvider, "TransportChannelProvider must not be null"); |
| 105 | + |
| 106 | + this.options = options; |
| 107 | + this.transportChannelProvider = transportChannelProvider; |
| 108 | + this.credentials = options.getCredentialSupplier().get(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public VaultToken login() throws VaultException { |
| 113 | + |
| 114 | + String signedJwt = signJwt(); |
| 115 | + |
| 116 | + return doLogin("GCP-IAM", signedJwt, this.options.getPath(), this.options.getRole()); |
| 117 | + } |
| 118 | + |
| 119 | + protected String signJwt() { |
| 120 | + |
| 121 | + String serviceAccount = getServiceAccountId(); |
| 122 | + Map<String, Object> jwtPayload = getJwtPayload(this.options, serviceAccount); |
| 123 | + |
| 124 | + try { |
| 125 | + IamCredentialsSettings credentialsSettings = IamCredentialsSettings.newBuilder() |
| 126 | + .setCredentialsProvider(() -> this.credentials) |
| 127 | + .setTransportChannelProvider(this.transportChannelProvider).build(); |
| 128 | + try (IamCredentialsClient iamCredentialsClient = IamCredentialsClient.create(credentialsSettings)) { |
| 129 | + String payload = JSON_FACTORY.toString(jwtPayload); |
| 130 | + ServiceAccountName serviceAccountName = ServiceAccountName.of("-", serviceAccount); |
| 131 | + SignJwtResponse response = iamCredentialsClient.signJwt(serviceAccountName, Collections.emptyList(), |
| 132 | + payload); |
| 133 | + return response.getSignedJwt(); |
| 134 | + } |
| 135 | + } |
| 136 | + catch (IOException e) { |
| 137 | + throw new VaultLoginException("Cannot sign JWT", e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private String getServiceAccountId() { |
| 142 | + return this.options.getServiceAccountIdAccessor().getServiceAccountId(this.credentials); |
| 143 | + } |
| 144 | + |
| 145 | + private static Map<String, Object> getJwtPayload(GcpIamCredentialsAuthenticationOptions options, |
| 146 | + String serviceAccount) { |
| 147 | + |
| 148 | + Instant validUntil = options.getClock().instant().plus(options.getJwtValidity()); |
| 149 | + |
| 150 | + Map<String, Object> payload = new LinkedHashMap<>(); |
| 151 | + |
| 152 | + payload.put("sub", serviceAccount); |
| 153 | + payload.put("aud", "vault/" + options.getRole()); |
| 154 | + payload.put("exp", validUntil.getEpochSecond()); |
| 155 | + |
| 156 | + return payload; |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments