snuyanzin commented on code in PR #30: URL: https://github.com/apache/flink-connector-gcp-pubsub/pull/30#discussion_r1818201370
########## flink-connector-gcp-pubsub/src/main/java/org/apache/flink/connector/gcp/pubsub/table/CredentialProviderFactory.java: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.gcp.pubsub.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.util.Preconditions; +import org.apache.flink.util.StringUtils; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.time.Instant; +import java.util.Arrays; +import java.util.Date; +import java.util.Map; +import java.util.Optional; + +import static com.google.cloud.pubsub.v1.SubscriptionAdminSettings.defaultCredentialsProviderBuilder; + +/** + * Factory for creating {@link com.google.api.gax.core.CredentialsProvider} from PubSub Table + * Options. + */ +@Internal +public class CredentialProviderFactory { + private static final String PUBSUB_AUTH_SCOPES = "https://www.googleapis.com/auth/pubsub"; + private static final String PUBSUB_AUTH_CLOUD_PLATFORM_SCOPES = + "https://www.googleapis.com/auth/cloud-platform"; + + public CredentialsProvider createCredentialsProvider( + CredentialProviderType credentialType, Map<String, String> options) { + switch (credentialType) { + case DEFAULT: + case GOOGLE_CREDENTIALS: + return defaultCredentialsProviderBuilder().build(); + case FIXED: + String missingOptionsErrorMessage = + "Fixed credential provider requires 'fixed.credentials-json' or 'fixed.credentials.access-token' " + + "and 'fixed.credentials.access-expiration-epoch-millis' options to be set"; + return Optional.ofNullable( + Optional.ofNullable( + getJsonCredentials( + options, missingOptionsErrorMessage)) + .orElse( + getAccessTokenCredentials( + options, missingOptionsErrorMessage))) + .map(FixedCredentialsProvider::create) + .orElseThrow( + () -> new IllegalArgumentException(missingOptionsErrorMessage)); + default: + throw new IllegalArgumentException( + "Unknown credential provider type: " + credentialType); + } + } + + private GoogleCredentials getJsonCredentials(Map<String, String> options, String errorMessage) { + Preconditions.checkNotNull(options, errorMessage); + String credentialsStringJson = options.get("fixed.credentials-json"); + if (StringUtils.isNullOrWhitespaceOnly(credentialsStringJson)) { + return null; + } + + try { + return GoogleCredentials.fromStream( + new ByteArrayInputStream(credentialsStringJson.getBytes())); + } catch (IOException e) { + throw new RuntimeException( + "Failed to load Google Credentials from file " + + options.get("fixed.credentials-json"), + e); + } + } + + private GoogleCredentials getAccessTokenCredentials( + Map<String, String> options, String errorMessage) { + Preconditions.checkNotNull(options, errorMessage); + String credentialsAccessToken = options.get("fixed.credentials.access-token"); Review Comment: can we have constants for such option names? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
