|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.gcp.auth; |
| 20 | + |
| 21 | +import com.google.auth.oauth2.GoogleCredentials; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.UncheckedIOException; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.iceberg.catalog.SessionCatalog; |
| 28 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 29 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 30 | +import org.apache.iceberg.relocated.com.google.common.base.Splitter; |
| 31 | +import org.apache.iceberg.relocated.com.google.common.base.Strings; |
| 32 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; |
| 33 | +import org.apache.iceberg.rest.RESTClient; |
| 34 | +import org.apache.iceberg.rest.auth.AuthManager; |
| 35 | +import org.apache.iceberg.rest.auth.AuthSession; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +/** |
| 40 | + * An authentication manager that uses Google Credentials (typically Application Default |
| 41 | + * Credentials) to create {@link GoogleAuthSession} instances. |
| 42 | + * |
| 43 | + * <p>This manager can be configured with properties such as: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li>{@code gcp.auth.credentials-path}: Path to a service account JSON key file. If not set, |
| 47 | + * Application Default Credentials will be used. |
| 48 | + * <li>{@code gcp.auth.scopes}: Comma-separated list of OAuth scopes to request. Defaults to |
| 49 | + * "https://www.googleapis.com/auth/cloud-platform". |
| 50 | + * </ul> |
| 51 | + */ |
| 52 | +public class GoogleAuthManager implements AuthManager { |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(GoogleAuthManager.class); |
| 54 | + private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings(); |
| 55 | + public static final String DEFAULT_SCOPES = "https://www.googleapis.com/auth/cloud-platform"; |
| 56 | + public static final String GCP_CREDENTIALS_PATH_PROPERTY = "gcp.auth.credentials-path"; |
| 57 | + public static final String GCP_SCOPES_PROPERTY = "gcp.auth.scopes"; |
| 58 | + private final String name; |
| 59 | + |
| 60 | + private GoogleCredentials credentials; |
| 61 | + private boolean initialized = false; |
| 62 | + |
| 63 | + public GoogleAuthManager(String managerName) { |
| 64 | + this.name = managerName; |
| 65 | + } |
| 66 | + |
| 67 | + public String name() { |
| 68 | + return name; |
| 69 | + } |
| 70 | + |
| 71 | + private void initialize(Map<String, String> properties) { |
| 72 | + if (initialized) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + String credentialsPath = properties.get(GCP_CREDENTIALS_PATH_PROPERTY); |
| 77 | + String scopesString = properties.getOrDefault(GCP_SCOPES_PROPERTY, DEFAULT_SCOPES); |
| 78 | + List<String> scopes = |
| 79 | + Strings.isNullOrEmpty(scopesString) |
| 80 | + ? ImmutableList.of() |
| 81 | + : ImmutableList.copyOf(SPLITTER.splitToList(scopesString)); |
| 82 | + |
| 83 | + try { |
| 84 | + if (credentialsPath != null && !credentialsPath.isEmpty()) { |
| 85 | + LOG.info("Using Google credentials from path: {}", credentialsPath); |
| 86 | + try (FileInputStream credentialsStream = new FileInputStream(credentialsPath)) { |
| 87 | + this.credentials = GoogleCredentials.fromStream(credentialsStream).createScoped(scopes); |
| 88 | + } |
| 89 | + } else { |
| 90 | + LOG.info("Using Application Default Credentials with scopes: {}", scopesString); |
| 91 | + this.credentials = GoogleCredentials.getApplicationDefault().createScoped(scopes); |
| 92 | + } |
| 93 | + } catch (IOException e) { |
| 94 | + throw new UncheckedIOException("Failed to load Google credentials", e); |
| 95 | + } |
| 96 | + |
| 97 | + this.initialized = true; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Initializes and returns a short-lived session, typically for fetching configuration. This |
| 102 | + * implementation reuses the long-lived catalog session logic. |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public AuthSession initSession(RESTClient initClient, Map<String, String> properties) { |
| 106 | + return catalogSession(initClient, properties); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns a long-lived session tied to the catalog's lifecycle. This session uses Google |
| 111 | + * Application Default Credentials or a specified service account. |
| 112 | + * |
| 113 | + * @param sharedClient The long-lived RESTClient (not used by this implementation for credential |
| 114 | + * fetching). |
| 115 | + * @param properties Configuration properties for the auth manager. |
| 116 | + * @return A {@link GoogleAuthSession}. |
| 117 | + * @throws UncheckedIOException if credential loading fails. |
| 118 | + */ |
| 119 | + @Override |
| 120 | + public AuthSession catalogSession(RESTClient sharedClient, Map<String, String> properties) { |
| 121 | + initialize(properties); |
| 122 | + Preconditions.checkState( |
| 123 | + credentials != null, "GoogleAuthManager not initialized or failed to load credentials"); |
| 124 | + return new GoogleAuthSession(credentials); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns a session for a specific context. Defaults to the catalog session. For GCP, tokens are |
| 129 | + * typically not context-specific in this manner. |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public AuthSession contextualSession(SessionCatalog.SessionContext context, AuthSession parent) { |
| 133 | + return parent; |
| 134 | + } |
| 135 | + |
| 136 | + /** Returns a session for a specific table or view. Defaults to the catalog session. */ |
| 137 | + @Override |
| 138 | + public AuthSession tableSession( |
| 139 | + TableIdentifier table, Map<String, String> properties, AuthSession parent) { |
| 140 | + return parent; |
| 141 | + } |
| 142 | + |
| 143 | + /** Closes the manager. This is a no-op for GoogleAuthManager. */ |
| 144 | + @Override |
| 145 | + public void close() {} |
| 146 | +} |
0 commit comments