Skip to content

Commit 7f6839f

Browse files
melaniedejongkurtisvg
authored andcommitted
Add service account enable/disable snippets (GoogleCloudPlatform#1590)
1 parent 6952315 commit 7f6839f

File tree

3 files changed

+184
-2
lines changed

3 files changed

+184
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_disable_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.DisableServiceAccountRequest;
25+
26+
import java.io.IOException;
27+
import java.security.GeneralSecurityException;
28+
import java.util.Collections;
29+
30+
public class DisableServiceAccount {
31+
32+
// Disables a service account.
33+
public static void disableServiceAccount(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+
DisableServiceAccountRequest request = new DisableServiceAccountRequest();
46+
service
47+
.projects()
48+
.serviceAccounts()
49+
.disable(
50+
"projects/-/serviceAccounts/"
51+
+ "your-service-account-name@"
52+
+ projectId
53+
+ ".iam.gserviceaccount.com",
54+
request)
55+
.execute();
56+
57+
System.out.println(
58+
"Disabled service account: "
59+
+ "your-service-account-name@"
60+
+ projectId
61+
+ ".iam.gserviceaccount.com");
62+
} catch (IOException e) {
63+
System.out.println("Unable to disable service account: \n" + e.toString());
64+
}
65+
}
66+
67+
private static Iam initService() throws GeneralSecurityException, IOException {
68+
// Use the Application Default Credentials strategy for authentication. For more info, see:
69+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
70+
GoogleCredential credential =
71+
GoogleCredential.getApplicationDefault()
72+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
73+
// Initialize the IAM service, which can be used to send requests to the IAM API.
74+
Iam service =
75+
new Iam.Builder(
76+
GoogleNetHttpTransport.newTrustedTransport(),
77+
JacksonFactory.getDefaultInstance(),
78+
credential)
79+
.setApplicationName("service-accounts")
80+
.build();
81+
return service;
82+
}
83+
}
84+
// [END iam_disable_service_account]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_enable_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.EnableServiceAccountRequest;
25+
26+
import java.io.IOException;
27+
import java.security.GeneralSecurityException;
28+
import java.util.Collections;
29+
30+
public class EnableServiceAccount {
31+
32+
// Enables a service account.
33+
public static void enableServiceAccount(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+
EnableServiceAccountRequest request = new EnableServiceAccountRequest();
46+
service
47+
.projects()
48+
.serviceAccounts()
49+
.enable(
50+
"projects/-/serviceAccounts/"
51+
+ "your-service-account-name@"
52+
+ projectId
53+
+ ".iam.gserviceaccount.com",
54+
request)
55+
.execute();
56+
57+
System.out.println(
58+
"Enabled service account: "
59+
+ "your-service-account-name@"
60+
+ projectId
61+
+ ".iam.gserviceaccount.com");
62+
} catch (IOException e) {
63+
System.out.println("Unable to enable service account: \n" + e.toString());
64+
}
65+
}
66+
67+
private static Iam initService() throws GeneralSecurityException, IOException {
68+
// Use the Application Default Credentials strategy for authentication. For more info, see:
69+
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
70+
GoogleCredential credential =
71+
GoogleCredential.getApplicationDefault()
72+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
73+
// Initialize the IAM service, which can be used to send requests to the IAM API.
74+
Iam service =
75+
new Iam.Builder(
76+
GoogleNetHttpTransport.newTrustedTransport(),
77+
JacksonFactory.getDefaultInstance(),
78+
credential)
79+
.setApplicationName("service-accounts")
80+
.build();
81+
return service;
82+
}
83+
}
84+
// [END iam_enable_service_account]

iam/api-client/src/test/java/com/google/iam/snippets/ServiceAccountTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,23 @@ public void stage3_testServiceAccountKeyDelete() {
103103
}
104104

105105
@Test
106-
public void stage4_testServiceAccountDelete() {
106+
public void stage4_testDisableServiceAccount() {
107+
DisableServiceAccount.disableServiceAccount(PROJECT_ID);
108+
String got = bout.toString();
109+
assertTrue(got.contains("Disabled service account:"));
110+
}
111+
112+
@Test
113+
public void stage5_testEnableServiceAccount() {
114+
EnableServiceAccount.enableServiceAccount(PROJECT_ID);
115+
String got = bout.toString();
116+
assertTrue(got.contains("Enabled service account:"));
117+
}
118+
119+
@Test
120+
public void stage6_testServiceAccountDelete() {
107121
DeleteServiceAccount.deleteServiceAccount(PROJECT_ID);
108122
String got = bout.toString();
109123
assertTrue(got.contains("Deleted service account:"));
110124
}
111-
}
125+
}

0 commit comments

Comments
 (0)