本文整理汇总了Java中com.google.api.client.util.SecurityUtils.loadPrivateKeyFromKeyStore方法的典型用法代码示例。如果您正苦于以下问题:Java SecurityUtils.loadPrivateKeyFromKeyStore方法的具体用法?Java SecurityUtils.loadPrivateKeyFromKeyStore怎么用?Java SecurityUtils.loadPrivateKeyFromKeyStore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.api.client.util.SecurityUtils
的用法示例。
在下文中一共展示了SecurityUtils.loadPrivateKeyFromKeyStore方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isValid
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
public boolean isValid() {
projectId = getSinkVar("project_id").trim();
datasetId = getSinkVar("dataset_id").trim();
serviceAccountId = getSinkVar("service_account_id").trim();
pk12KeyBase64 = getSinkVar("pk12base64").trim();
// Init key
try {
byte[] keyBytes = Base64.decodeBase64(pk12KeyBase64);
ByteArrayInputStream bis = new ByteArrayInputStream(keyBytes);
pk12 = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), bis, "notasecret", "privatekey", "notasecret");
LOG.info("Loaded PK12 key");
} catch (Exception e) {
LOG.error("Failed to load private key", e);
return false;
}
return !projectId.isEmpty() && !datasetId.isEmpty() && !serviceAccountId.isEmpty();
}
示例2: authorize
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
private Credential authorize() throws IOException, GeneralSecurityException
{
PrivateKey securityKey = SecurityUtils.loadPrivateKeyFromKeyStore
(
SecurityUtils.getPkcs12KeyStore(),
ClassUtils.getDefaultClassLoader().getResourceAsStream(credentials_.getLoginCredentials().getCredential()),
"notasecret", "privatekey", "notasecret"
);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(),
new FileReader(new File(SystemUtils2.getApplicationDataPath(), credentials_.getLoginCredentials().getIdentity())));
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId((String) clientSecrets.getWeb().get("client_email"))
.setServiceAccountScopes(Arrays.asList(ComputeScopes.COMPUTE, ComputeScopes.DEVSTORAGE_FULL_CONTROL))
.setServiceAccountPrivateKey(securityKey)
.build();
return credential;
}
示例3: Parser
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
/**
* Initialize the parser by setting up the BigQuery client ready to take take commands.
*
* @throws GeneralSecurityException
* @throws IOException
*/
private Parser() throws GeneralSecurityException, IOException {
final HttpTransport http = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = new JacksonFactory();
final KeyStore keyStore = SecurityUtils.getPkcs12KeyStore();
final InputStream key = getClass().getClassLoader().getResourceAsStream(keyFile);
final PrivateKey p12 = SecurityUtils.loadPrivateKeyFromKeyStore(keyStore, key, "notasecret", "privatekey", "notasecret");
final GoogleCredential.Builder credential = new GoogleCredential.Builder();
credential.setTransport(http);
credential.setJsonFactory(jsonFactory);
credential.setServiceAccountId(account);
credential.setServiceAccountScopes(Arrays.asList(BigqueryScopes.BIGQUERY));
credential.setServiceAccountPrivateKey(p12);
final Bigquery.Builder bq = new Bigquery.Builder(http, jsonFactory, credential.build());
bq.setApplicationName(project);
client = bq.build();
}
示例4: getCredentialFromPrivateKeyServiceAccount
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
/**
* Initializes OAuth2 credential from a private keyfile, as described in
* <a href="https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_Accounts"
* > OAuth2 Service Accounts</a>.
*
* @param serviceAccountEmail Email address of the service account associated with the keyfile.
* @param privateKeyFile Full local path to private keyfile.
* @param scopes List of well-formed desired scopes to use with the credential.
*/
public static Credentials getCredentialFromPrivateKeyServiceAccount(
String serviceAccountEmail, String privateKeyFile, List<String> scopes)
throws IOException, GeneralSecurityException {
String clientId = null;
String privateKeyId = null;
PrivateKey privateKey =
SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new FileInputStream(privateKeyFile), "notasecret", "privatekey", "notasecret");
return new ServiceAccountCredentials(clientId, serviceAccountEmail, privateKey, privateKeyId,
scopes);
}
示例5: createCredentialForServiceAccount
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
/**
* see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
* https://developers.google.com/console/help/#activatingapis <---- look for [Service accounts]!!!!
* https://developers.google.com/accounts/docs/OAuth2ServiceAccount <---- look for [Service accounts]!!!!
* @param httpTransport
* @param jsonFactory
* @return
* @throws IOException
* @throws GeneralSecurityException
*/
@SuppressWarnings("resource")
public static GoogleCredential createCredentialForServiceAccount(final HttpTransport httpTransport,
final JsonFactory jsonFactory,
final GoogleAPIServiceAccountClientData serviceAccountClientId) throws IOException,
GeneralSecurityException {
// Load the private key from the p12 store
PrivateKey serviceAccountPrivateKey = null;
@Cleanup InputStream privateKeyIS = null;
if (serviceAccountClientId.getP12KeyPath().isLoadedFromClassPath()) {
privateKeyIS = Thread.currentThread().getContextClassLoader().getResourceAsStream(serviceAccountClientId.getP12KeyPath().getFilePathAsString());
} else {
File p12KeyFile = new File(serviceAccountClientId.getP12KeyPath().getFilePathAsString());
privateKeyIS = new FileInputStream(p12KeyFile);
}
if (privateKeyIS == null) throw new IllegalStateException(Throwables.message("Could NOT load the P12 key file from {} using {} loader",
serviceAccountClientId.getP12KeyPath().getFilePathAsString(),
serviceAccountClientId.getP12KeyPath().getResourcesLoaderType()));
serviceAccountPrivateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
privateKeyIS,
"notasecret",
"privatekey",
"notasecret");
// create the credential
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountClientId.getClientEmail().asString())
.setServiceAccountPrivateKey(serviceAccountPrivateKey)
.setServiceAccountScopes(serviceAccountClientId.getScopes()) // see https://developers.google.com/gmail/api/auth/scopes
.setServiceAccountUser(serviceAccountClientId.getEndUserEmail().asString())
.build();
credential.refreshToken();
return credential;
}
示例6: buildCredential
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
public static GoogleCredential buildCredential(CloudCredential gcpCredential, HttpTransport httpTransport) throws IOException, GeneralSecurityException {
PrivateKey pk = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new ByteArrayInputStream(Base64.decodeBase64(getServiceAccountPrivateKey(gcpCredential))), "notasecret", "privatekey", "notasecret");
return new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(getServiceAccountId(gcpCredential))
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKey(pk)
.build();
}
示例7: deleteNetwork
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
@AfterSuite
@Parameters("vpcName")
public void deleteNetwork(@Optional("it-vpc") String vpcName) throws Exception {
springTestContextPrepareTestInstance();
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(defaultServiceAccountId)
.setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
.setServiceAccountPrivateKey(privateKey)
.build();
Compute compute = new Builder(httpTransport, jsonFactory, null)
.setApplicationName(defaultName)
.setHttpRequestInitializer(googleCredential)
.build();
Delete delete = compute.networks().delete(defaultProjectId, vpcName);
Operation operation = delete.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new IllegalStateException("gcp operation failed: " + operation.getHttpErrorMessage());
}
}
示例8: checkTagsGcp
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
protected static void checkTagsGcp(ApplicationContext applicationContext, String applicationName, String projectId, String serviceAccountId, String p12File,
String availabilityZone, List<String> instanceIdList, Map<String, String> tagsToCheckMap) throws Exception {
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, p12File);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountId)
.setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
.setServiceAccountPrivateKey(privateKey)
.build();
Compute compute = new Builder(httpTransport, jsonFactory, null)
.setApplicationName(applicationName)
.setHttpRequestInitializer(googleCredential)
.build();
Instances instances = compute.instances();
for (String id : instanceIdList) {
Get response = instances.get(projectId, availabilityZone, id);
com.google.api.services.compute.model.Instance instance = response.execute();
Tags gcpTags = instance.getTags();
Map<String, String> extractedTags = new HashMap<>();
List<String> tagList = gcpTags.getItems();
for (String i : tagList) {
String[] tmpTagList = i.split("-");
if (tmpTagList.length > 1) {
extractedTags.put(tmpTagList[0], tmpTagList[1]);
}
}
checkTags(tagsToCheckMap, extractedTags);
extractedTags.clear();
}
}
示例9: createNetwork
import com.google.api.client.util.SecurityUtils; //导入方法依赖的package包/类
@Test
@Parameters({"networkName", "description", "publicInAccount", "resourceGroupName", "vpcName", "vpcSubnet", "subnetCIDR", "networkType"})
public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount,
@Optional("europe-west1") String subnetRegion, @Optional("it-vpc") String vpcName,
@Optional("it-vpc-subnet") String vpcSubnet, @Optional("10.0.36.0/24") String subnetCIDR, NetworkType networkType) throws Exception {
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(defaultServiceAccountId)
.setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE))
.setServiceAccountPrivateKey(privateKey)
.build();
Compute compute = new Builder(httpTransport, jsonFactory, null)
.setApplicationName(defaultName)
.setHttpRequestInitializer(googleCredential)
.build();
Network gcpNetwork = new Network();
gcpNetwork.setName(vpcName);
if (!LAGACY_NETWORK.equals(networkType)) {
gcpNetwork.setAutoCreateSubnetworks(false);
}
Networks.Insert networkInsert = compute.networks().insert(defaultProjectId, gcpNetwork);
Operation networkInsertResponse = networkInsert.execute();
if (networkInsertResponse.getHttpErrorStatusCode() != null) {
throw new IllegalStateException("gcp network operation failed: " + networkInsertResponse.getHttpErrorMessage());
}
waitOperation(compute, networkInsertResponse);
if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
Subnetwork gcpSubnet = new Subnetwork();
gcpSubnet.setName(vpcSubnet);
gcpSubnet.setIpCidrRange(subnetCIDR);
gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", defaultProjectId, vpcName));
Insert subNetworkInsert = compute.subnetworks().insert(defaultProjectId, subnetRegion, gcpSubnet);
Operation subNetInsertResponse = subNetworkInsert.execute();
if (subNetInsertResponse.getHttpErrorStatusCode() != null) {
throw new IllegalStateException("gcp subnetwork operation failed: " + subNetInsertResponse.getHttpErrorMessage());
}
}
NetworkRequest networkRequest = new NetworkRequest();
networkRequest.setName(networkName);
networkRequest.setDescription(description);
if (NEW_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
networkRequest.setSubnetCIDR(subnetCIDR);
}
Map<String, Object> map = new HashMap<>();
map.put("networkId", vpcName);
if (EXISTING_SUBNET_IN_EXISTING_NETWORK.equals(networkType)) {
map.put("subnetId", vpcSubnet);
}
networkRequest.setParameters(map);
networkRequest.setCloudPlatform("GCP");
String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString();
getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true);
}