-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore: IAM Validation added for fail-safe. #3072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (C) 2025 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.teleport.spanner.iam; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** Represents the result of an IAM permission check on a specific resource. */ | ||
| public class IAMCheckResult { | ||
| private final String resourceName; | ||
| private final List<String> missingPermissions; | ||
|
|
||
| public IAMCheckResult(String resourceName, List<String> missingPermissions) { | ||
| this.resourceName = resourceName; | ||
| this.missingPermissions = new ArrayList<>(missingPermissions); | ||
| } | ||
|
|
||
| public String getResourceName() { | ||
| return resourceName; | ||
| } | ||
|
|
||
| public List<String> getMissingPermissions() { | ||
| return new ArrayList<>(missingPermissions); | ||
| } | ||
|
|
||
| public boolean isPermissionsAvailable() { | ||
| return missingPermissions.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "IAMCheckResult{" | ||
| + "resourceName='" | ||
| + resourceName | ||
| + '\'' | ||
| + ", missingPermissions=" | ||
| + missingPermissions | ||
| + ", success=" | ||
| + isPermissionsAvailable() | ||
| + '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * Copyright (C) 2025 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.teleport.spanner.iam; | ||
|
|
||
| import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
| import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
| import com.google.api.client.http.HttpRequestInitializer; | ||
| import com.google.api.client.http.HttpTransport; | ||
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.api.client.json.jackson2.JacksonFactory; | ||
| import com.google.api.services.cloudresourcemanager.v3.CloudResourceManager; | ||
| import com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsRequest; | ||
| import com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsResponse; | ||
| import com.google.auth.Credentials; | ||
| import com.google.auth.http.HttpCredentialsAdapter; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.beam.sdk.extensions.gcp.auth.NullCredentialInitializer; | ||
| import org.apache.beam.sdk.extensions.gcp.options.GcpOptions; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Utility to check IAM permissions for various GCP resources. */ | ||
| public class IAMPermissionsChecker { | ||
| private static final Logger LOG = LoggerFactory.getLogger(IAMPermissionsChecker.class); | ||
| private final Credentials credential; | ||
| private static final String RESOURCE_NAME_FORMAT = "projects/%s"; | ||
| private final String projectIdResource; | ||
|
|
||
| private final CloudResourceManager resourceManager; | ||
|
|
||
| public IAMPermissionsChecker(String projectId, GcpOptions gcpOptions) | ||
| throws GeneralSecurityException, IOException { | ||
| this.credential = gcpOptions.getGcpCredential(); | ||
| this.projectIdResource = String.format("projects/%s", projectId); | ||
| resourceManager = createCloudResourceManagerService(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| IAMPermissionsChecker( | ||
| String projectId, GcpOptions gcpOptions, CloudResourceManager resourceManager) { | ||
| this.credential = gcpOptions.getGcpCredential(); | ||
| this.projectIdResource = String.format("projects/%s", projectId); | ||
| this.resourceManager = resourceManager; | ||
| } | ||
|
|
||
| /** | ||
| * Checks IAM permissions for a list of requirements. This api should be called once with all the | ||
| * requirements. | ||
| * | ||
| * @param requirements List of resources and required permissions. | ||
| * @return List of results, only missing permissions are included. Empty list indicate all the | ||
| * requirements are met. | ||
| */ | ||
| public IAMCheckResult check(List<IAMResourceRequirements> requirements) { | ||
| List<String> permissionList = | ||
| requirements.stream() | ||
| .map(IAMResourceRequirements::getPermissions) | ||
| .flatMap(Collection::stream) | ||
| .toList(); | ||
| HashSet<String> grantedPermissions = | ||
| new HashSet<>(checkPermission(resourceManager, projectIdResource, permissionList)); | ||
|
|
||
| List<String> missingPermissions = | ||
| permissionList.stream() | ||
| .filter(p -> !grantedPermissions.contains(p)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new IAMCheckResult(projectIdResource, missingPermissions); | ||
| } | ||
|
|
||
| private List<String> checkPermission( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the difference between this and previous "check" method? their signature looks similar
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This require resource name. This was made from the perspective when we are checking permission at resource level. Currently, there isn't much difference.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like a helper method. In which case, it might make sense to call the other method from here (or vice versa) to not duplicate the flows. |
||
| CloudResourceManager resourceManager, String resourceName, List<String> permissions) { | ||
| try { | ||
|
|
||
| TestIamPermissionsRequest requestBody = | ||
| new TestIamPermissionsRequest().setPermissions(permissions); | ||
|
|
||
| TestIamPermissionsResponse testIamPermissionsResponse = | ||
| resourceManager.projects().testIamPermissions(resourceName, requestBody).execute(); | ||
|
|
||
| List<String> granted = testIamPermissionsResponse.getPermissions(); | ||
| return granted == null ? Collections.emptyList() : granted; | ||
| } catch (IOException e) { | ||
| LOG.error("Error checking permissions for resource {}", resourceName, e); | ||
| throw new RuntimeException("Failed to check project permissions", e); | ||
| } | ||
| } | ||
|
|
||
| private CloudResourceManager createCloudResourceManagerService() | ||
| throws IOException, GeneralSecurityException { | ||
| HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | ||
| JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); | ||
| HttpRequestInitializer initializer = getHttpRequestInitializer(this.credential); | ||
| CloudResourceManager service = | ||
| new CloudResourceManager.Builder(httpTransport, jsonFactory, initializer) | ||
| .setApplicationName("service-accounts") | ||
| .build(); | ||
| return service; | ||
| } | ||
|
|
||
| private static HttpRequestInitializer getHttpRequestInitializer(Credentials credential) | ||
| throws IOException { | ||
| if (credential == null) { | ||
| try { | ||
| return GoogleCredential.getApplicationDefault(); | ||
| } catch (Exception e) { | ||
| return new NullCredentialInitializer(); | ||
| } | ||
| } else { | ||
| return new HttpCredentialsAdapter(credential); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see this code is duplicated across pipelines. would it make sense to push it down as an API in the IAM checker module?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs to be per-Template. For Import and export the code is same. But this will change when we shift to other templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you pl help be understand better with an example, on how it might deviate? and may be leave a comment for future code readers