Search in sources :

Example 1 with JSONWebToken

use of com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken in project photon-model by vmware.

the class GCPEnumerationAdapterService method getAccessToken.

/**
 * Method to get the access token to send RESTful APIs later.
 * Every access token is only valid for an hour.
 * @param ctx The Enumeration Context.
 * @param clientEmail The client email in service account's credential file.
 * @param scopes The limitation of application's access.
 * @param privateKey The key generated by private key in service account's credential file.
 * @throws GeneralSecurityException The exception will be thrown when private key is invalid.
 * @throws IOException The exception will be thrown when inputs are mal-formatted.
 */
private void getAccessToken(EnumerationContext ctx, String clientEmail, Collection<String> scopes, PrivateKey privateKey, EnumerationStages next) throws GeneralSecurityException, IOException {
    Consumer<GCPAccessTokenResponse> onSuccess = response -> {
        ctx.accessToken = response.access_token;
        ctx.stage = next;
        handleEnumerationRequest(ctx);
    };
    JSONWebToken jwt = new JSONWebToken(clientEmail, scopes, privateKey);
    String assertion = jwt.getAssertion();
    GCPUtils.getAccessToken(this, assertion, onSuccess, getFailureConsumer(ctx));
}
Also used : GCPUriPaths(com.vmware.photon.controller.model.adapters.gcp.GCPUriPaths) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) GCPUtils.privateKeyFromPkcs8(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.privateKeyFromPkcs8) QueryTask(com.vmware.xenon.services.common.QueryTask) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) GCPInstance(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) DiskType(com.vmware.photon.controller.model.resources.DiskService.DiskType) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) ENVIRONMENT_NAME_GCP(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_GCP) Utils(com.vmware.xenon.common.Utils) GeneralSecurityException(java.security.GeneralSecurityException) GCPInstancesList(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstancesList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) URI(java.net.URI) DEFAULT_DISK_SOURCE_IMAGE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SOURCE_IMAGE) GCPUtils.assignPowerState(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignPowerState) GCPUtils.extractActualInstanceType(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractActualInstanceType) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DISK_AUTO_DELETE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DISK_AUTO_DELETE) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_DISK_SERVICE_REFERENCE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SERVICE_REFERENCE) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GCPDisk(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPDisk) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) LIST_VM_TEMPLATE_URI(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.LIST_VM_TEMPLATE_URI) UUID(java.util.UUID) ServiceHost(com.vmware.xenon.common.ServiceHost) List(java.util.List) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.AUTH_HEADER_BEARER_PREFIX) PrivateKey(java.security.PrivateKey) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) DiskService(com.vmware.photon.controller.model.resources.DiskService) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) GCPUtils(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils) MAX_RESULTS(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.MAX_RESULTS) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PAGE_TOKEN(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.PAGE_TOKEN) UriPaths(com.vmware.photon.controller.model.UriPaths) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) LinkedList(java.util.LinkedList) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) GCPUtils.extractRegionFromZone(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractRegionFromZone) EncryptionUtils(com.vmware.photon.controller.model.security.util.EncryptionUtils) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) ComputeScopes(com.google.api.services.compute.ComputeScopes) Consumer(java.util.function.Consumer) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) DEFAULT_DISK_CAPACITY(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_CAPACITY) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) GCPUtils.assignIPAddress(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignIPAddress) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse)

Example 2 with JSONWebToken

use of com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken in project photon-model by vmware.

the class GCPStatsService method getAccessToken.

/**
 * Gets the access token required for making requests to the monitoring API.
 * @param statsData The GCPStatsDataHolder instance containing statsRequest.
 * @param nextStage The next stage of StatsCollectionStage for the service.
 * @throws GeneralSecurityException
 * @throws IOException
 */
private void getAccessToken(GCPStatsDataHolder statsData, StatsCollectionStage nextStage) throws GeneralSecurityException, IOException {
    Consumer<GCPAccessTokenResponse> onSuccess = (response) -> {
        statsData.accessToken = response.access_token;
        statsData.stage = nextStage;
        handleStatsRequest(statsData);
    };
    JSONWebToken jwt = new JSONWebToken(statsData.userEmail, GCPConstants.SCOPES, GCPUtils.privateKeyFromPkcs8(statsData.privateKey));
    String assertion = jwt.getAssertion();
    GCPUtils.getAccessToken(this, assertion, onSuccess, getFailureConsumer(statsData));
}
Also used : GCPUriPaths(com.vmware.photon.controller.model.adapters.gcp.GCPUriPaths) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) SimpleDateFormat(java.text.SimpleDateFormat) GCPUtils(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ArrayList(java.util.ArrayList) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) GeneralSecurityException(java.security.GeneralSecurityException) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GCPMetricResponse(com.vmware.photon.controller.model.adapters.gcp.podo.stats.GCPMetricResponse) URI(java.net.URI) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ParseException(java.text.ParseException) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) TimeZone(java.util.TimeZone) EncryptionUtils(com.vmware.photon.controller.model.security.util.EncryptionUtils) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) IOException(java.io.IOException) TimeSeries(com.vmware.photon.controller.model.adapters.gcp.podo.stats.TimeSeries) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) GCPStatsNormalizer(com.vmware.photon.controller.model.adapters.gcp.utils.GCPStatsNormalizer) GCPConstants(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) UriUtils(com.vmware.xenon.common.UriUtils) Collections(java.util.Collections) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse)

Aggregations

GCPUriPaths (com.vmware.photon.controller.model.adapters.gcp.GCPUriPaths)2 GCPAccessTokenResponse (com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse)2 GCPUtils (com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils)2 JSONWebToken (com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken)2 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)2 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)2 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)2 ComputeScopes (com.google.api.services.compute.ComputeScopes)1 CUSTOM_OS_TYPE (com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE)1 OSType (com.vmware.photon.controller.model.ComputeProperties.OSType)1 UriPaths (com.vmware.photon.controller.model.UriPaths)1 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)1 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)1 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)1 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)1 GCPConstants (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants)1 AUTH_HEADER_BEARER_PREFIX (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.AUTH_HEADER_BEARER_PREFIX)1 DEFAULT_DISK_CAPACITY (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_CAPACITY)1 DEFAULT_DISK_SERVICE_REFERENCE (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SERVICE_REFERENCE)1 DEFAULT_DISK_SOURCE_IMAGE (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SOURCE_IMAGE)1