use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageHmacKeySnippets method testListHmacKeys.
@Test
public void testListHmacKeys() {
// Create 2 HMAC keys
storage.createHmacKey(ServiceAccount.of(HMAC_KEY_TEST_SERVICE_ACCOUNT), Storage.CreateHmacKeyOption.projectId(PROJECT_ID));
storage.createHmacKey(ServiceAccount.of(HMAC_KEY_TEST_SERVICE_ACCOUNT), Storage.CreateHmacKeyOption.projectId(PROJECT_ID));
Page<HmacKeyMetadata> page = storageSnippets.listHmacKeys(PROJECT_ID);
int count = 0;
for (HmacKeyMetadata metadata : page.iterateAll()) {
if (metadata.getServiceAccount().getEmail().equals(HMAC_KEY_TEST_SERVICE_ACCOUNT)) {
count++;
}
}
assertEquals(2, count);
}
use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageHmacKeySnippets method testDeleteHmacKey.
@Test
public void testDeleteHmacKey() {
HmacKey hmacKey = storage.createHmacKey(ServiceAccount.of(HMAC_KEY_TEST_SERVICE_ACCOUNT));
HmacKeyMetadata metadata = storage.updateHmacKeyState(hmacKey.getMetadata(), HmacKeyState.INACTIVE);
storageSnippets.deleteHmacKey(metadata.getAccessId(), PROJECT_ID);
}
use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageHmacKeySnippets method testGetHmacKey.
@Test
public void testGetHmacKey() {
HmacKey hmacKey = storage.createHmacKey(ServiceAccount.of(HMAC_KEY_TEST_SERVICE_ACCOUNT));
HmacKeyMetadata metadata = storageSnippets.getHmacKey(hmacKey.getMetadata().getAccessId(), PROJECT_ID);
assertNotNull(metadata);
}
use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method createHmacKey.
/**
* Example of creating an HMAC key for a service account *
*/
public HmacKey createHmacKey(String serviceAccountEmail, String projectId) throws StorageException {
// [START storage_create_hmac_key]
// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();
// The service account email for which the new HMAC key will be created.
// String serviceAccountEmail = "service-account@iam.gserviceaccount.com";
//
// The ID of the project to which the service account belongs.
// String projectId = "project-id";
ServiceAccount account = ServiceAccount.of(serviceAccountEmail);
HmacKey hmacKey = storage.createHmacKey(account, Storage.CreateHmacKeyOption.projectId(projectId));
String secret = hmacKey.getSecretKey();
HmacKeyMetadata metadata = hmacKey.getMetadata();
System.out.println("The Base64 encoded secret is: " + secret);
System.out.println("Do not miss that secret, there is no API to recover it.");
System.out.println("The HMAC key metadata is:");
System.out.println("ID: " + metadata.getId());
System.out.println("Access ID: " + metadata.getAccessId());
System.out.println("Project ID: " + metadata.getProjectId());
System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
System.out.println("State: " + metadata.getState().toString());
System.out.println("Time Created: " + new Date(metadata.getCreateTime()).toString());
System.out.println("Time Updated: " + new Date(metadata.getUpdateTime()).toString());
System.out.println("ETag: " + metadata.getEtag());
// [END storage_create_hmac_key]
return hmacKey;
}
use of com.google.cloud.storage.HmacKey.HmacKeyMetadata in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method deactivateHmacKey.
/**
* Example of deactivating an HMAC key. *
*/
public HmacKeyMetadata deactivateHmacKey(String accessId, String projectId) throws StorageException {
// [START storage_deactivate_hmac_key]
// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();
// The access ID of the HMAC key, e.g. "GOOG0234230X00"
// String accessId = "GOOG0234230X00";
//
// The ID of the project to which the service account belongs.
// String projectId = "project-id";
HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
HmacKeyMetadata newMetadata = storage.updateHmacKeyState(metadata, HmacKeyState.INACTIVE);
System.out.println("The HMAC key is now inactive.");
System.out.println("The HMAC key metadata is:");
System.out.println("ID: " + newMetadata.getId());
System.out.println("Access ID: " + newMetadata.getAccessId());
System.out.println("Project ID: " + newMetadata.getProjectId());
System.out.println("Service Account Email: " + newMetadata.getServiceAccount().getEmail());
System.out.println("State: " + newMetadata.getState().toString());
System.out.println("Time Created: " + new Date(newMetadata.getCreateTime()).toString());
System.out.println("Time Updated: " + new Date(newMetadata.getUpdateTime()).toString());
System.out.println("ETag: " + newMetadata.getEtag());
// [END storage_deactivate_hmac_key]
return newMetadata;
}
Aggregations