Search in sources :

Example 1 with STATUS_CODE_UNAUTHORIZED

use of com.vmware.xenon.common.Operation.STATUS_CODE_UNAUTHORIZED in project photon-model by vmware.

the class AzureEndpointAdapterService method validate.

private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate(EndpointConfigRequest body) {
    return (credentials, callback) -> {
        try {
            Boolean shouldProvision = Boolean.parseBoolean(body.endpointProperties.get(AZURE_PROVISIONING_PERMISSION));
            validateEndpointUniqueness(credentials, body.checkForEndpointUniqueness, body.tenantLinks).thenCompose(aVoid -> validateCredentials(credentials)).thenCompose(subscription -> getPermissions(credentials)).thenCompose(permList -> verifyPermissions(permList, shouldProvision)).whenComplete((aVoid, e) -> {
                if (e == null) {
                    callback.accept(null, null);
                    return;
                }
                if (e instanceof CompletionException) {
                    e = e.getCause();
                }
                final LocalizableValidationException localizableExc;
                if (e instanceof LocalizableValidationException) {
                    localizableExc = (LocalizableValidationException) e;
                } else {
                    // Azure doesn't send us any meaningful status code to work with
                    localizableExc = new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
                }
                ServiceErrorResponse rsp = Utils.toServiceErrorResponse(localizableExc);
                rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
                callback.accept(rsp, localizableExc);
            });
        } catch (Throwable e) {
            logSevere(e);
            ServiceErrorResponse rsp = new ServiceErrorResponse();
            rsp.message = "Invalid Azure credentials";
            rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
            callback.accept(rsp, e);
        }
    };
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) QueryTask(com.vmware.xenon.services.common.QueryTask) PROVIDER_PERMISSIONS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_PERMISSIONS_URI) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubscriptionInner(com.microsoft.azure.management.resources.implementation.SubscriptionInner) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE) URI(java.net.URI) Permission(com.vmware.photon.controller.model.adapters.azure.model.permission.Permission) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) AUTHORIZATION_NAMESPACE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTHORIZATION_NAMESPACE) PROVIDER_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_REST_API_VERSION) CompletionException(java.util.concurrent.CompletionException) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) STATUS_CODE_UNAUTHORIZED(com.vmware.xenon.common.Operation.STATUS_CODE_UNAUTHORIZED) Optional(java.util.Optional) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) PermissionList(com.vmware.photon.controller.model.adapters.azure.model.permission.PermissionList) PRIVATE_KEY_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY) SubscriptionState(com.microsoft.azure.management.resources.SubscriptionState) USER_LINK_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.USER_LINK_KEY) HashMap(java.util.HashMap) PRIVATE_KEYID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY) SubscriptionClientImpl(com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl) ArrayList(java.util.ArrayList) ZONE_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ZONE_KEY) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) BiConsumer(java.util.function.BiConsumer) Retriever(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils.Retriever) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) EndpointAdapterUtils(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils) AZURE_TENANT_ID(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_TENANT_ID) SUPPORT_DATASTORES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES) AZURE_PROVISIONING_PERMISSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_PROVISIONING_PERMISSION) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) REGION_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY) SUPPORT_PUBLIC_IMAGES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) CompletionException(java.util.concurrent.CompletionException) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse)

Aggregations

SubscriptionState (com.microsoft.azure.management.resources.SubscriptionState)1 SubscriptionClientImpl (com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl)1 SubscriptionInner (com.microsoft.azure.management.resources.implementation.SubscriptionInner)1 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)1 PRIVATE_KEYID_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY)1 PRIVATE_KEY_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY)1 REGION_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY)1 RequestType (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType)1 SUPPORT_DATASTORES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES)1 SUPPORT_PUBLIC_IMAGES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES)1 USER_LINK_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.USER_LINK_KEY)1 ZONE_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ZONE_KEY)1 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)1 AUTHORIZATION_NAMESPACE (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTHORIZATION_NAMESPACE)1 AUTH_HEADER_BEARER_PREFIX (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX)1 AZURE_PROVISIONING_PERMISSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_PROVISIONING_PERMISSION)1 AZURE_TENANT_ID (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_TENANT_ID)1 PROVIDER_PERMISSIONS_URI (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_PERMISSIONS_URI)1 PROVIDER_REST_API_VERSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_REST_API_VERSION)1 QUERY_PARAM_API_VERSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION)1