GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-docs-samples/kms/src/main/java/com/example/Snippets.java at master · phunl/java-docs-samples Expand file tree/ Copy pathSnippets.javaMore file actions 551 lines (471 loc) · 18.4 KB / Copy pathSnippets.javaFile metadata and controls551 lines (471 loc) · 18.4 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 /* * Copyright (c) 2017 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. */ package com.example; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudkms.v1.CloudKMS; import com.google.api.services.cloudkms.v1.CloudKMSScopes; import com.google.api.services.cloudkms.v1.model.Binding; import com.google.api.services.cloudkms.v1.model.CryptoKey; import com.google.api.services.cloudkms.v1.model.CryptoKeyVersion; import com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest; import com.google.api.services.cloudkms.v1.model.KeyRing; import com.google.api.services.cloudkms.v1.model.ListCryptoKeyVersionsResponse; import com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse; import com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse; import com.google.api.services.cloudkms.v1.model.Policy; import com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import java.io.IOException; import java.util.Collections; import java.util.List; public class Snippets { /** * Creates an authorized CloudKMS client service using Application Default Credentials. * * @return an authorized CloudKMS client * @throws IOException if there's an error getting the default credentials. */ public static CloudKMS createAuthorizedClient() throws IOException { // Create the credential HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); // Authorize the client using Application Default Credentials // @see https://g.co/dv/identity/protocols/application-default-credentials GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); // Depending on the environment that provides the default credentials (e.g. Compute Engine, App // Engine), the credentials may require us to specify the scopes we need explicitly. // Check for this case, and inject the scope if required. if (credential.createScopedRequired()) { credential = credential.createScoped(CloudKMSScopes.all()); } return new CloudKMS.Builder(transport, jsonFactory, credential) .setApplicationName("CloudKMS snippets") .build(); } // [START kms_create_keyring] /** * Creates a new key ring with the given id. */ public static KeyRing createKeyRing(String projectId, String ringId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the location associated with the KeyRing. String parent = String.format("projects/%s/locations/%s", projectId, location); // Create the KeyRing for your project. KeyRing keyring = kms.projects().locations().keyRings() .create(parent, new KeyRing()) .setKeyRingId(ringId) .execute(); System.out.println(keyring); return keyring; } // [END kms_create_keyring] // [START kms_create_cryptokey] /** * Creates a new crypto key with the given id. */ public static CryptoKey createCryptoKey(String projectId, String ringId, String keyId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the location associated with the KeyRing. String parent = String.format( "projects/%s/locations/%s/keyRings/%s", projectId, location, ringId); // This will allow the API access to the key for encryption and decryption. String purpose = "ENCRYPT_DECRYPT"; CryptoKey cryptoKey = new CryptoKey(); cryptoKey.setPurpose(purpose); // Create the CryptoKey for your project. CryptoKey createdKey = kms.projects().locations().keyRings().cryptoKeys() .create(parent, cryptoKey) .setCryptoKeyId(keyId) .execute(); System.out.println(createdKey); return createdKey; } // [END kms_create_cryptokey] // [START kms_create_cryptokey_version] /** * Creates a new crypto key version for the given id. */ public static void createCryptoKeyVersion( String projectId, String ringId, String keyId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String cryptoKeys = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, location, ringId, keyId); CryptoKeyVersion version = new CryptoKeyVersion(); CryptoKeyVersion newVersion = kms.projects().locations().keyRings().cryptoKeys() .cryptoKeyVersions() .create(cryptoKeys, version) .execute(); System.out.println(newVersion); } // [END kms_create_cryptokey_version] // [START kms_disable_cryptokey_version] /** * Disables the given version of the crypto key. */ public static CryptoKeyVersion disableCryptoKeyVersion( String projectId, String ringId, String keyId, String version) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey version String cryptoKeyVersion = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s", projectId, location, ringId, keyId, version); CryptoKeyVersion newVersionState = new CryptoKeyVersion() .setState("DISABLED"); CryptoKeyVersion response = kms.projects().locations().keyRings().cryptoKeys() .cryptoKeyVersions() .patch(cryptoKeyVersion, newVersionState) .setUpdateMask("state") .execute(); System.out.println(response); return response; } // [END kms_disable_cryptokey_version] // [START kms_destroy_cryptokey_version] /** * Marks the given version of a crypto key to be destroyed at a scheduled future point. */ public static CryptoKeyVersion destroyCryptoKeyVersion( String projectId, String ringId, String keyId, String version) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey version String cryptoKeyVersion = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s", projectId, location, ringId, keyId, version); DestroyCryptoKeyVersionRequest destroyRequest = new DestroyCryptoKeyVersionRequest(); CryptoKeyVersion destroyed = kms.projects().locations().keyRings().cryptoKeys() .cryptoKeyVersions() .destroy(cryptoKeyVersion, destroyRequest) .execute(); System.out.println(destroyed); return destroyed; } // [END kms_destroy_cryptokey_version] // [START kms_get_cryptokey_policy] /** * Retrieves the IAM policy for the given crypto key. */ public static Policy getCryptoKeyPolicy(String projectId, String ringId, String keyId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String cryptoKey = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, location, ringId, keyId); // Get the current IAM policy and add the new account to it. Policy iamPolicy = kms.projects().locations().keyRings().cryptoKeys() .getIamPolicy(cryptoKey) .execute(); System.out.println(iamPolicy.getBindings()); return iamPolicy; } // [END kms_get_cryptokey_policy] // [START kms_get_keyring_policy] /** * Retrieves the IAM policy for the given crypto key. */ public static Policy getKeyRingPolicy(String projectId, String ringId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the keyring String keyring = String.format( "projects/%s/locations/%s/keyRings/%s", projectId, location, ringId); // Get the current IAM policy and add the new account to it. Policy iamPolicy = kms.projects().locations().keyRings() .getIamPolicy(keyring) .execute(); System.out.println(iamPolicy.getBindings()); return iamPolicy; } // [END kms_get_keyring_policy] // [START kms_add_member_to_cryptokey_policy] /** * Adds the given member to the given key, with the given role. * * @param ringId The id of the keyring. * @param keyId The id of the crypto key. * @param member The member to add. Must be in the proper format, eg: * * allUsers * user:$userEmail * serviceAccount:$serviceAccountEmail * * See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding * for more details. * * @param role Must be in one of the following formats: * roles/[role] * organizations/[organizationId]/roles/[role] * projects/[projectId]/roles/[role] * * See https://g.co/cloud/iam/docs/understanding-roles * for available values for [role]. */ public static Policy addMemberToCryptoKeyPolicy( String projectId, String ringId, String keyId, String member, String role) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey version String cryptoKey = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, location, ringId, keyId); // Get the current IAM policy Policy iamPolicy = getCryptoKeyPolicy(projectId, ringId, keyId); // Add the new account to it. Binding newBinding = new Binding() .setRole(role) .setMembers(Collections.singletonList(member)); List<Binding> bindings = iamPolicy.getBindings(); if (null == bindings) { bindings = Collections.singletonList(newBinding); } else { bindings.add(newBinding); } iamPolicy.setBindings(bindings); // Set the new IAM Policy. Policy newIamPolicy = kms.projects().locations().keyRings() .cryptoKeys() .setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)) .execute(); System.out.println("Response: " + newIamPolicy); return newIamPolicy; } // [END kms_add_member_to_cryptokey_policy] // [START kms_add_member_to_keyring_policy] /** * Adds the given member to the given keyring, with the given role. * * @param ringId The id of the keyring. * @param member The member to add. Must be in the proper format, eg: * * allUsers * user:$userEmail * serviceAccount:$serviceAccountEmail * * See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding * for more details. * * @param role Must be in one of the following formats: * roles/[role] * organizations/[organizationId]/roles/[role] * projects/[projectId]/roles/[role] * * See https://g.co/cloud/iam/docs/understanding-roles * for available values for [role]. */ public static Policy addMemberToKeyRingPolicy( String projectId, String ringId, String member, String role) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the keyring version String keyring = String.format( "projects/%s/locations/%s/keyRings/%s", projectId, location, ringId); // Get the current IAM policy Policy iamPolicy = getKeyRingPolicy(projectId, ringId); // Add the new account to it. Binding newBinding = new Binding() .setRole(role) .setMembers(Collections.singletonList(member)); List<Binding> bindings = iamPolicy.getBindings(); if (null == bindings) { bindings = Collections.singletonList(newBinding); } else { bindings.add(newBinding); } iamPolicy.setBindings(bindings); // Set the new IAM Policy. Policy newIamPolicy = kms.projects().locations() .keyRings() .setIamPolicy(keyring, new SetIamPolicyRequest().setPolicy(iamPolicy)) .execute(); System.out.println("Response: " + newIamPolicy); return newIamPolicy; } // [END kms_add_member_to_keyring_policy] // [START kms_remove_member_from_cryptokey_policy] /** * Removes the given member from the given policy. */ public static Policy removeMemberFromCryptoKeyPolicy( String projectId, String ringId, String keyId, String member, String role) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String cryptoKey = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, location, ringId, keyId); // Get the current IAM policy and add the new account to it. Policy iamPolicy = getCryptoKeyPolicy(projectId, ringId, keyId); if (null == iamPolicy.getBindings()) { // Nothing to remove return null; } // Filter out the given member for (Binding b : iamPolicy.getBindings()) { if (role.equals(b.getRole()) && b.getMembers().contains(member)) { b.getMembers().removeAll(Collections.singletonList(member)); break; } } // Set the new IAM Policy. Policy newIamPolicy = kms.projects().locations().keyRings() .cryptoKeys() .setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)) .execute(); System.out.println("Response: " + newIamPolicy); return newIamPolicy; } // [END kms_remove_member_from_cryptokey_policy] // [START kms_remove_member_from_keyring_policy] /** * Removes the given member from the given policy. */ public static Policy removeMemberFromKeyRingPolicy( String projectId, String ringId, String member, String role) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String cryptoKey = String.format( "projects/%s/locations/%s/keyRings/%s", projectId, location, ringId); // Get the current IAM policy and add the new account to it. Policy iamPolicy = getKeyRingPolicy(projectId, ringId); // Filter out the given member for (Binding b : iamPolicy.getBindings()) { if (role.equals(b.getRole()) && b.getMembers().contains(member)) { b.getMembers().remove(member); break; } } // Set the new IAM Policy. Policy newIamPolicy = kms.projects().locations() .keyRings() .setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)) .execute(); System.out.println("Response: " + newIamPolicy); return newIamPolicy; } // [END kms_remove_member_from_keyring_policy] /** * Prints all the keyrings in the given project. */ public static void listKeyRings(String projectId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String keyRingPath = String.format( "projects/%s/locations/%s", projectId, location); // Make the RPC call ListKeyRingsResponse response = kms.projects().locations() .keyRings() .list(keyRingPath) .execute(); // Print the returned key rings if (null != response.getKeyRings()) { System.out.println("Key Rings: "); for (KeyRing keyRing : response.getKeyRings()) { System.out.println(keyRing.getName()); } } else { System.out.println("No keyrings defined."); } } /** * Prints all the keys in the given key ring. */ public static void listCryptoKeys(String projectId, String ringId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String keyRingPath = String.format( "projects/%s/locations/%s/keyRings/%s", projectId, location, ringId); ListCryptoKeysResponse cryptoKeys = kms.projects().locations().keyRings() .cryptoKeys() .list(keyRingPath) .execute(); for (CryptoKey key : cryptoKeys.getCryptoKeys()) { System.out.println(key); } } /** * Prints all the versions for the given crypto key. */ public static void listCryptoKeyVersions( String projectId, String ringId, String keyId) throws IOException { String location = "global"; // Create the Cloud KMS client. CloudKMS kms = createAuthorizedClient(); // The resource name of the cryptoKey String cryptoKeys = String.format( "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, location, ringId, keyId); DestroyCryptoKeyVersionRequest destroyRequest = new DestroyCryptoKeyVersionRequest(); ListCryptoKeyVersionsResponse versions = kms.projects().locations().keyRings().cryptoKeys() .cryptoKeyVersions() .list(cryptoKeys) .execute(); for (CryptoKeyVersion version : versions.getCryptoKeyVersions()) { System.out.println(version); } } public static void main(String[] args) throws IOException, CmdLineException { SnippetCommands commands = new SnippetCommands(); CmdLineParser parser = new CmdLineParser(commands); try { parser.parseArgument(args); } catch (CmdLineException e) { System.out.println(e); System.out.println(); e.getParser().printUsage(System.err); System.exit(1); } commands.command.run(); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |