Skip to content

Commit 6952315

Browse files
melaniedejongkurtisvg
authored andcommitted
Updating IAM service account snippets (GoogleCloudPlatform#1578)
1 parent d3f4e13 commit 6952315

10 files changed

Lines changed: 648 additions & 242 deletions
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
// [START iam_create_service_account]
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.services.iam.v1.Iam;
23+
import com.google.api.services.iam.v1.IamScopes;
24+
import com.google.api.services.iam.v1.model.CreateServiceAccountRequest;
25+
import com.google.api.services.iam.v1.model.ServiceAccount;
26+
import java.io.IOException;
27+
import java.security.GeneralSecurityException;
28+
import java.util.Collections;
29+
30+
public class CreateServiceAccount {
31+
32+
// Creates a service account.
33+
public static void createServiceAccount(String projectId) {
34+
// String projectId = "my-project-id";
35+
36+
Iam service = null;
37+
try {
38+
service = initService();
39+
} catch (IOException | GeneralSecurityException e) {
40+
System.out.println("Unable to initialize service: \n" + e.toString());
41+
return;
42+
}
43+
44+
try {
45+
ServiceAccount serviceAccount = new ServiceAccount();
46+
serviceAccount.setDisplayName("your-display-name");
47+
CreateServiceAccountRequest request = new CreateServiceAccountRequest();
48+
request.setAccountId("your-service-account-name");
49+
request.setServiceAccount(serviceAccount);
50+
51+
serviceAccount =
52+
service.projects().serviceAccounts().create("projects/" + projectId, request).execute();
53+
54+
System.out.println("Created service account: " + serviceAccount.getEmail());
55+
} catch (IOException e) {
56+
System.out.println("Unable to create service account: \n" + e.toString());
57+
}
58+
}
59+
60+
private static Iam initService() throws GeneralSecurityException, IOException {
61+
// Use the Application Default Credentials strategy for authentication. For more info, see:
62+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
63+
GoogleCredential credential =
64+
GoogleCredential.getApplicationDefault()
65+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
66+
// Initialize the IAM service, which can be used to send requests to the IAM API.
67+
Iam service =
68+
new Iam.Builder(
69+
GoogleNetHttpTransport.newTrustedTransport(),
70+
JacksonFactory.getDefaultInstance(),
71+
credential)
72+
.setApplicationName("service-accounts")
73+
.build();
74+
return service;
75+
}
76+
}
77+
// [END iam_create_service_account]

iam/api-client/src/main/java/com/google/iam/snippets/ServiceAccountKeys.java renamed to iam/api-client/src/main/java/com/google/iam/snippets/CreateServiceAccountKey.java

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2018 Google LLC
1+
/* Copyright 2019 Google LLC
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515

1616
package com.google.iam.snippets;
1717

18+
// [START iam_create_key]
1819
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
1920
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
2021
import com.google.api.client.json.jackson2.JacksonFactory;
@@ -23,70 +24,57 @@
2324
import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest;
2425
import com.google.api.services.iam.v1.model.ServiceAccountKey;
2526
import java.io.IOException;
27+
import java.security.GeneralSecurityException;
2628
import java.util.Collections;
27-
import java.util.List;
2829

29-
public class ServiceAccountKeys {
30+
public class CreateServiceAccountKey {
3031

31-
private final Iam service;
32+
// Creates a key for a service account.
33+
public static void createKey(String projectId) {
34+
// String projectId = "my-project-id";
35+
36+
Iam service = null;
37+
try {
38+
service = initService();
39+
} catch (IOException | GeneralSecurityException e) {
40+
System.out.println("Unable to initialize service: \n" + e.toString());
41+
return;
42+
}
43+
44+
try {
45+
ServiceAccountKey key =
46+
service
47+
.projects()
48+
.serviceAccounts()
49+
.keys()
50+
.create(
51+
"projects/-/serviceAccounts/your-service-account-name@"
52+
+ projectId
53+
+ ".iam.gserviceaccount.com",
54+
new CreateServiceAccountKeyRequest())
55+
.execute();
56+
57+
System.out.println("Created key: " + key.getName());
58+
} catch (IOException e) {
59+
System.out.println("Unable to create service account key: \n" + e.toString());
60+
}
61+
}
3262

33-
public ServiceAccountKeys() throws Exception {
63+
private static Iam initService() throws GeneralSecurityException, IOException {
64+
// Use the Application Default Credentials strategy for authentication. For more info, see:
65+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
3466
GoogleCredential credential =
3567
GoogleCredential.getApplicationDefault()
3668
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
37-
38-
service =
69+
// Initialize the IAM service, which can be used to send requests to the IAM API.
70+
Iam service =
3971
new Iam.Builder(
4072
GoogleNetHttpTransport.newTrustedTransport(),
4173
JacksonFactory.getDefaultInstance(),
4274
credential)
4375
.setApplicationName("service-account-keys")
4476
.build();
77+
return service;
4578
}
46-
47-
// [START iam_create_key]
48-
public ServiceAccountKey createKey(String serviceAccountEmail) throws IOException {
49-
50-
ServiceAccountKey key =
51-
service
52-
.projects()
53-
.serviceAccounts()
54-
.keys()
55-
.create(
56-
"projects/-/serviceAccounts/" + serviceAccountEmail,
57-
new CreateServiceAccountKeyRequest())
58-
.execute();
59-
60-
System.out.println("Created key: " + key.getName());
61-
return key;
62-
}
63-
// [END iam_create_key]
64-
65-
// [START iam_list_keys]
66-
public List<ServiceAccountKey> listKeys(String serviceAccountEmail) throws IOException {
67-
68-
List<ServiceAccountKey> keys =
69-
service
70-
.projects()
71-
.serviceAccounts()
72-
.keys()
73-
.list("projects/-/serviceAccounts/" + serviceAccountEmail)
74-
.execute()
75-
.getKeys();
76-
77-
for (ServiceAccountKey key : keys) {
78-
System.out.println("Key: " + key.getName());
79-
}
80-
return keys;
81-
}
82-
// [END iam_list_keys]
83-
84-
// [START iam_delete_key]
85-
public void deleteKey(String fullKeyName) throws IOException {
86-
87-
service.projects().serviceAccounts().keys().delete(fullKeyName).execute();
88-
89-
System.out.println("Deleted key: " + fullKeyName);
90-
}
91-
// [END iam_delete_key]
9279
}
80+
// [END iam_create_key]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
// [START iam_delete_service_account]
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.services.iam.v1.Iam;
23+
import com.google.api.services.iam.v1.IamScopes;
24+
import java.io.IOException;
25+
import java.security.GeneralSecurityException;
26+
import java.util.Collections;
27+
28+
public class DeleteServiceAccount {
29+
30+
// Deletes a service account.
31+
public static void deleteServiceAccount(String projectId) {
32+
// String projectId = "my-project-id";
33+
34+
Iam service = null;
35+
try {
36+
service = initService();
37+
} catch (IOException | GeneralSecurityException e) {
38+
System.out.println("Unable to initialize service: \n" + e.toString());
39+
return;
40+
}
41+
42+
try {
43+
service
44+
.projects()
45+
.serviceAccounts()
46+
.delete(
47+
"projects/-/serviceAccounts/"
48+
+ "your-service-account-name@"
49+
+ projectId
50+
+ ".iam.gserviceaccount.com")
51+
.execute();
52+
53+
System.out.println(
54+
"Deleted service account: "
55+
+ "your-service-account-name@"
56+
+ projectId
57+
+ ".iam.gserviceaccount.com");
58+
} catch (IOException e) {
59+
System.out.println("Unable to delete service account: \n" + e.toString());
60+
}
61+
}
62+
63+
private static Iam initService() throws GeneralSecurityException, IOException {
64+
// Use the Application Default Credentials strategy for authentication. For more info, see:
65+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
66+
GoogleCredential credential =
67+
GoogleCredential.getApplicationDefault()
68+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
69+
// Initialize the IAM service, which can be used to send requests to the IAM API.
70+
Iam service =
71+
new Iam.Builder(
72+
GoogleNetHttpTransport.newTrustedTransport(),
73+
JacksonFactory.getDefaultInstance(),
74+
credential)
75+
.setApplicationName("service-accounts")
76+
.build();
77+
return service;
78+
}
79+
}
80+
// [END iam_delete_service_account]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
// [START iam_delete_key]
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.services.iam.v1.Iam;
23+
import com.google.api.services.iam.v1.IamScopes;
24+
import com.google.api.services.iam.v1.model.ServiceAccountKey;
25+
import java.io.IOException;
26+
import java.security.GeneralSecurityException;
27+
import java.util.Collections;
28+
import java.util.List;
29+
30+
public class DeleteServiceAccountKey {
31+
32+
// Deletes a service account key.
33+
public static void deleteKey(String projectId) {
34+
// String projectId = "my-project-id";
35+
36+
Iam service = null;
37+
try {
38+
service = initService();
39+
} catch (IOException | GeneralSecurityException e) {
40+
System.out.println("Unable to initialize service: \n" + e.toString());
41+
return;
42+
}
43+
44+
try {
45+
// First, get the name of the key using List() or Get()
46+
List<ServiceAccountKey> keys =
47+
service
48+
.projects()
49+
.serviceAccounts()
50+
.keys()
51+
.list(
52+
"projects/-/serviceAccounts/"
53+
+ "your-service-account-name@"
54+
+ projectId
55+
+ ".iam.gserviceaccount.com")
56+
.execute()
57+
.getKeys();
58+
String keyToDelete = keys.get(0).getName();
59+
60+
// Then you can delete the key
61+
service.projects().serviceAccounts().keys().delete(keyToDelete).execute();
62+
63+
System.out.println("Deleted key: " + keyToDelete);
64+
} catch (IOException e) {
65+
System.out.println("Unable to delete service account key: \n" + e.toString());
66+
}
67+
}
68+
69+
private static Iam initService() throws GeneralSecurityException, IOException {
70+
// Use the Application Default Credentials strategy for authentication. For more info, see:
71+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
72+
GoogleCredential credential =
73+
GoogleCredential.getApplicationDefault()
74+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
75+
// Initialize the IAM service, which can be used to send requests to the IAM API.
76+
Iam service =
77+
new Iam.Builder(
78+
GoogleNetHttpTransport.newTrustedTransport(),
79+
JacksonFactory.getDefaultInstance(),
80+
credential)
81+
.setApplicationName("service-account-keys")
82+
.build();
83+
return service;
84+
}
85+
}
86+
// [END iam_delete_key]

0 commit comments

Comments
 (0)