-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add IAM quickstart * Add service accounts * Add service account keys and tests * Remove unused imports * Add grantable roles * Lint code * Change project env variable for tests * Update project env variable for tests * Reformat license * Add output verification to tests * Fix tests * Lint license and tests, update pom version
- Loading branch information
1 parent
68f12df
commit 85ef6f2
Showing
9 changed files
with
603 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Cloud Identity & Access Management Samples | ||
|
||
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iam/api-client/README.md"> | ||
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> | ||
|
||
[Google Cloud Identity & Access Management](https://cloud.google.com/iam/) (IAM) | ||
lets administrators authorize who can take action on specific resources. | ||
These sample applications demonstrate how to interact with Cloud IAM using | ||
the Google API Client Library for Java. | ||
|
||
## Quickstart | ||
|
||
Install [Maven](http://maven.apache.org/). | ||
|
||
Build the project with: | ||
|
||
```xml | ||
mvn clean package | ||
``` | ||
|
||
Run the Quickstart, which lists roles in a project: | ||
|
||
```xml | ||
mvn exec:java | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!-- | ||
Copyright 2018 Google Inc. | ||
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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.iam.snippets</groupId> | ||
<artifactId>iam-snippets</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0</version> | ||
<name>iam-snippets</name> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.10</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-iam</artifactId> | ||
<version>v1-rev247-1.23.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.40</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.4.0</version> | ||
<configuration> | ||
<mainClass>com.google.iam.snippets.GrantableRoles</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
60 changes: 60 additions & 0 deletions
60
iam/api-client/src/main/java/com/google/iam/snippets/GrantableRoles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Copyright 2018 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.iam.snippets; | ||
|
||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.iam.v1.Iam; | ||
import com.google.api.services.iam.v1.IamScopes; | ||
import com.google.api.services.iam.v1.model.QueryGrantableRolesRequest; | ||
import com.google.api.services.iam.v1.model.QueryGrantableRolesResponse; | ||
import com.google.api.services.iam.v1.model.Role; | ||
import java.util.Collections; | ||
|
||
public class GrantableRoles { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
GoogleCredential credential = | ||
GoogleCredential.getApplicationDefault() | ||
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); | ||
|
||
Iam service = | ||
new Iam.Builder( | ||
GoogleNetHttpTransport.newTrustedTransport(), | ||
JacksonFactory.getDefaultInstance(), | ||
credential) | ||
.setApplicationName("grantable-roles") | ||
.build(); | ||
|
||
String fullResourceName = args[0]; | ||
|
||
// [START iam_view_grantable_roles] | ||
QueryGrantableRolesRequest request = new QueryGrantableRolesRequest(); | ||
request.setFullResourceName(fullResourceName); | ||
|
||
QueryGrantableRolesResponse response = service.roles().queryGrantableRoles(request).execute(); | ||
|
||
for (Role role : response.getRoles()) { | ||
System.out.println("Title: " + role.getTitle()); | ||
System.out.println("Name: " + role.getName()); | ||
System.out.println("Description: " + role.getDescription()); | ||
System.out.println(); | ||
} | ||
// [START iam_view_grantable_roles] | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
iam/api-client/src/main/java/com/google/iam/snippets/Quickstart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Copyright 2018 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. | ||
*/ | ||
|
||
// [START iam_quickstart] | ||
|
||
package com.google.iam.snippets; | ||
|
||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.iam.v1.Iam; | ||
import com.google.api.services.iam.v1.IamScopes; | ||
import com.google.api.services.iam.v1.model.ListRolesResponse; | ||
import com.google.api.services.iam.v1.model.Role; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Quickstart { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Get credentials | ||
GoogleCredential credential = | ||
GoogleCredential.getApplicationDefault() | ||
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); | ||
|
||
// Create the Cloud IAM service object | ||
Iam service = | ||
new Iam.Builder( | ||
GoogleNetHttpTransport.newTrustedTransport(), | ||
JacksonFactory.getDefaultInstance(), | ||
credential) | ||
.setApplicationName("quickstart") | ||
.build(); | ||
|
||
// Call the Cloud IAM Roles API | ||
ListRolesResponse respose = service.roles().list().execute(); | ||
List<Role> roles = respose.getRoles(); | ||
|
||
// Process the response | ||
for (Role role : roles) { | ||
System.out.println("Title: " + role.getTitle()); | ||
System.out.println("Name: " + role.getName()); | ||
System.out.println("Description: " + role.getDescription()); | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
// [END iam_quickstart] |
92 changes: 92 additions & 0 deletions
92
iam/api-client/src/main/java/com/google/iam/snippets/ServiceAccountKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* Copyright 2018 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.iam.snippets; | ||
|
||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.iam.v1.Iam; | ||
import com.google.api.services.iam.v1.IamScopes; | ||
import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest; | ||
import com.google.api.services.iam.v1.model.ServiceAccountKey; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ServiceAccountKeys { | ||
|
||
private final Iam service; | ||
|
||
public ServiceAccountKeys() throws Exception { | ||
GoogleCredential credential = | ||
GoogleCredential.getApplicationDefault() | ||
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); | ||
|
||
service = | ||
new Iam.Builder( | ||
GoogleNetHttpTransport.newTrustedTransport(), | ||
JacksonFactory.getDefaultInstance(), | ||
credential) | ||
.setApplicationName("service-account-keys") | ||
.build(); | ||
} | ||
|
||
// [START iam_create_key] | ||
public ServiceAccountKey createKey(String serviceAccountEmail) throws IOException { | ||
|
||
ServiceAccountKey key = | ||
service | ||
.projects() | ||
.serviceAccounts() | ||
.keys() | ||
.create( | ||
"projects/-/serviceAccounts/" + serviceAccountEmail, | ||
new CreateServiceAccountKeyRequest()) | ||
.execute(); | ||
|
||
System.out.println("Created key: " + key.getName()); | ||
return key; | ||
} | ||
// [END iam_create_key] | ||
|
||
// [START iam_list_keys] | ||
public List<ServiceAccountKey> listKeys(String serviceAccountEmail) throws IOException { | ||
|
||
List<ServiceAccountKey> keys = | ||
service | ||
.projects() | ||
.serviceAccounts() | ||
.keys() | ||
.list("projects/-/serviceAccounts/" + serviceAccountEmail) | ||
.execute() | ||
.getKeys(); | ||
|
||
for (ServiceAccountKey key : keys) { | ||
System.out.println("Key: " + key.getName()); | ||
} | ||
return keys; | ||
} | ||
// [END iam_list_keys] | ||
|
||
// [START iam_delete_key] | ||
public void deleteKey(String fullKeyName) throws IOException { | ||
|
||
service.projects().serviceAccounts().keys().delete(fullKeyName).execute(); | ||
|
||
System.out.println("Deleted key: " + fullKeyName); | ||
} | ||
// [END iam_delete_key] | ||
} |
Oops, something went wrong.