Search in sources :

Example 6 with Gateway

use of org.apache.airavata.model.workspace.Gateway in project airavata by apache.

the class RegistryServiceDBEventHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    logger.info("RegistryServiceDBEventHandler | Received a new message!");
    try {
        // construct dbeventmessage thrift datamodel
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        logger.info("RegistryService received db-event-message from publisher: " + dbEventMessage.getPublisherService());
        // get publisher context
        DBEventPublisherContext publisherContext = dbEventMessage.getMessageContext().getPublisher().getPublisherContext();
        logger.info("RegistryService, Replicated Entity: " + publisherContext.getEntityType());
        // this try-block is mainly for catching DuplicateEntryException
        try {
            // check type of entity-type
            switch(publisherContext.getEntityType()) {
                // Gateway related operations
                case TENANT:
                    {
                        // construct gateway datamodel from message
                        Gateway gateway = new Gateway();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), gateway);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addGateway in Registry.");
                                    registryClient.addGateway(gateway);
                                    logger.info("addGateway Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    if (!registryClient.isGatewayExist(gateway.getGatewayId())) {
                                        logger.info("Gateway doesn't exist so adding instead of updating.");
                                        registryClient.addGateway(gateway);
                                    } else {
                                        registryClient.updateGateway(gateway.getGatewayId(), gateway);
                                    }
                                    logger.info("updateGateway Replication Success!");
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    registryClient.deleteGateway(gateway.getGatewayId());
                                    logger.info("deleteGateway Replication Success!");
                                    break;
                                }
                        }
                        // break entity: gateway
                        break;
                    }
                // UserProfile related operations
                case USER_PROFILE:
                    {
                        // construct userprofile datamodel from message
                        UserProfile userProfile = new UserProfile();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), userProfile);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addUser in Registry.");
                                    registryClient.addUser(userProfile);
                                    logger.info("addUser Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                        }
                        // break entity: userprofile
                        break;
                    }
                // no handler for entity
                default:
                    {
                        logger.error("Handler not defined for Entity: " + publisherContext.getEntityType());
                    }
            }
        } catch (DuplicateEntryException ex) {
            // log this exception and proceed (do nothing)
            // this exception is thrown mostly when messages are re-consumed, hence ignore
            logger.warn("DuplicateEntryException while consuming db-event message, ex: " + ex.getMessage(), ex);
        }
        // send ack for received message
        logger.info("RegistryServiceDBEventHandler | Sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag());
        RegistryServiceDBEventMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (TException ex) {
        logger.error("Error processing message: " + ex, ex);
    } catch (ApplicationSettingsException ex) {
        logger.error("Error fetching application settings: " + ex, ex);
    } catch (AiravataException ex) {
        logger.error("Error sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag(), ex);
    }
}
Also used : TException(org.apache.thrift.TException) DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) DBEventPublisherContext(org.apache.airavata.model.dbevent.DBEventPublisherContext) Gateway(org.apache.airavata.model.workspace.Gateway) DuplicateEntryException(org.apache.airavata.model.error.DuplicateEntryException) AiravataException(org.apache.airavata.common.exception.AiravataException)

Example 7 with Gateway

use of org.apache.airavata.model.workspace.Gateway in project airavata by apache.

the class SecureClient method main.

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    // register client or use existing client
    System.out.println("");
    System.out.println("Please select from the following options:");
    System.out.println("1. Register the client as an OAuth application.");
    System.out.println("2. Client is already registered. Use the existing credentials.");
    String opInput = scanner.next();
    int option = Integer.valueOf(opInput.trim());
    String consumerId = null;
    String consumerSecret = null;
    if (option == 1) {
        // register OAuth application - this happens once during initialization of the gateway.
        /**
         **********************Start obtaining input from user****************************
         */
        System.out.println("");
        System.out.println("Registering an OAuth application representing the client....");
        System.out.println("Please enter following information as you prefer, or use defaults.");
        System.out.println("OAuth application name: (default:" + Properties.appName + ", press 'd' to use default value.)");
        String appNameInput = scanner.next();
        String appName = null;
        if (appNameInput.trim().equals("d")) {
            appName = Properties.appName;
        } else {
            appName = appNameInput.trim();
        }
        System.out.println("Consumer Id: (default:" + Properties.consumerID + ", press 'd' to use default value.)");
        String consumerIdInput = scanner.next();
        if (consumerIdInput.trim().equals("d")) {
            consumerId = Properties.consumerID;
        } else {
            consumerId = consumerIdInput.trim();
        }
        System.out.println("Consumer Secret: (default:" + Properties.consumerSecret + ", press 'd' to use default value.)");
        String consumerSecInput = scanner.next();
        if (consumerSecInput.trim().equals("d")) {
            consumerSecret = Properties.consumerSecret;
        } else {
            consumerSecret = consumerSecInput.trim();
        }
        /**
         ********************* Perform registration of the client as an OAuth app**************************
         */
        try {
            ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
            OAuthAppRegisteringClient authAppRegisteringClient = new OAuthAppRegisteringClient(Properties.oauthAuthzServerURL, Properties.adminUserName, Properties.adminPassword, configContext);
            OAuthConsumerAppDTO appDTO = authAppRegisteringClient.registerApplication(appName, consumerId, consumerSecret);
            /**
             ******************* Complete registering the client **********************************************
             */
            System.out.println("");
            System.out.println("Registered OAuth app successfully. Following is app's details:");
            System.out.println("App Name: " + appDTO.getApplicationName());
            System.out.println("Consumer ID: " + appDTO.getOauthConsumerKey());
            System.out.println("Consumer Secret: " + appDTO.getOauthConsumerSecret());
            System.out.println("");
        } catch (AiravataSecurityException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    } else if (option == 2) {
        System.out.println("");
        System.out.println("Enter Consumer Id: ");
        consumerId = scanner.next().trim();
        System.out.println("Enter Consumer Secret: ");
        consumerSecret = scanner.next().trim();
    }
    // obtain OAuth access token
    /**
     **********************Start obtaining input from user****************************
     */
    System.out.println("");
    System.out.println("Please select the preferred grant type: (or press d to use the default option" + Properties.grantType + ")");
    System.out.println("1. Resource Owner Password Credential.");
    System.out.println("2. Client Credential.");
    String grantTypeInput = scanner.next().trim();
    int grantType = 0;
    if (grantTypeInput.equals("d")) {
        grantType = Properties.grantType;
    } else {
        grantType = Integer.valueOf(grantTypeInput);
    }
    String userName = null;
    String password = null;
    if (grantType == 1) {
        System.out.println("Obtaining OAuth access token via 'Resource Owner Password' grant type....");
        System.out.println("Please enter following information as you prefer, or use defaults.");
        System.out.println("End user's name: (default:" + Properties.userName + ", press 'd' to use default value.)");
        String userNameInput = scanner.next();
        if (userNameInput.trim().equals("d")) {
            userName = Properties.userName;
        } else {
            userName = userNameInput.trim();
        }
        System.out.println("End user's password: (default:" + Properties.password + ", press 'd' to use default value.)");
        String passwordInput = scanner.next();
        if (passwordInput.trim().equals("d")) {
            password = Properties.password;
        } else {
            password = passwordInput.trim();
        }
    } else if (grantType == 2) {
        System.out.println("");
        System.out.println("Please enter the user name to be passed: ");
        String userNameInput = scanner.next();
        userName = userNameInput.trim();
        System.out.println("");
        System.out.println("Obtaining OAuth access token via 'Client Credential' grant type...' grant type....");
    }
    /**
     *************************** Finish obtaining input from user******************************************
     */
    try {
        // obtain the OAuth token for the specified end user.
        String accessToken = new OAuthTokenRetrievalClient().retrieveAccessToken(consumerId, consumerSecret, userName, password, grantType);
        System.out.println("");
        System.out.println("OAuth access token is: " + accessToken);
        // invoke Airavata API by the SecureClient, on behalf of the user.
        System.out.println("");
        System.out.println("Invoking Airavata API...");
        System.out.println("Enter the access token to be used: (default:" + accessToken + ", press 'd' to use default value.)");
        String accessTokenInput = scanner.next();
        String acTk = null;
        if (accessTokenInput.trim().equals("d")) {
            acTk = accessToken;
        } else {
            acTk = accessTokenInput.trim();
        }
        // obtain as input, the method to be invoked
        System.out.println("");
        System.out.println("Enter the number corresponding to the method to be invoked: ");
        System.out.println("1. getAPIVersion");
        System.out.println("2. getAllAppModules");
        System.out.println("3. addGateway");
        String methodNumberString = scanner.next();
        int methodNumber = Integer.valueOf(methodNumberString.trim());
        Airavata.Client client = createAiravataClient(Properties.SERVER_HOST, Properties.SERVER_PORT);
        AuthzToken authzToken = new AuthzToken();
        authzToken.setAccessToken(acTk);
        Map<String, String> claimsMap = new HashMap<>();
        claimsMap.put("userName", userName);
        claimsMap.put("email", "hasini@gmail.com");
        authzToken.setClaimsMap(claimsMap);
        if (methodNumber == 1) {
            String version = client.getAPIVersion(authzToken);
            System.out.println("");
            System.out.println("Airavata API version: " + version);
            System.out.println("");
        } else if (methodNumber == 2) {
            System.out.println("");
            System.out.println("Enter the gateway id: ");
            String gatewayId = scanner.next().trim();
            List<ApplicationModule> appModules = client.getAllAppModules(authzToken, gatewayId);
            System.out.println("Output of getAllAppModuels: ");
            for (ApplicationModule appModule : appModules) {
                System.out.println(appModule.getAppModuleName());
            }
            System.out.println("");
            System.out.println("");
        } else if (methodNumber == 3) {
            System.out.println("");
            System.out.println("Enter the gateway id: ");
            String gatewayId = scanner.next().trim();
            Gateway gateway = new Gateway(gatewayId, GatewayApprovalStatus.REQUESTED);
            gateway.setDomain("airavata.org");
            gateway.setEmailAddress("airavata@apache.org");
            gateway.setGatewayName("airavataGW");
            String output = client.addGateway(authzToken, gateway);
            System.out.println("");
            System.out.println("Output of addGateway: " + output);
            System.out.println("");
        }
    } catch (InvalidRequestException e) {
        e.printStackTrace();
    } catch (TException e) {
        e.printStackTrace();
    } catch (AiravataSecurityException e) {
        e.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) Scanner(java.util.Scanner) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) HashMap(java.util.HashMap) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO) TException(org.apache.thrift.TException) InvalidRequestException(org.apache.airavata.model.error.InvalidRequestException) AiravataClientException(org.apache.airavata.model.error.AiravataClientException) AiravataSecurityException(org.apache.airavata.security.AiravataSecurityException) ApplicationModule(org.apache.airavata.model.appcatalog.appdeployment.ApplicationModule) Gateway(org.apache.airavata.model.workspace.Gateway) AuthzToken(org.apache.airavata.model.security.AuthzToken) List(java.util.List) InvalidRequestException(org.apache.airavata.model.error.InvalidRequestException) AiravataSecurityException(org.apache.airavata.security.AiravataSecurityException) Airavata(org.apache.airavata.api.Airavata)

Example 8 with Gateway

use of org.apache.airavata.model.workspace.Gateway in project airavata by apache.

the class IamAdminServicesHandler method setUpGateway.

@Override
@SecurityCheck
public Gateway setUpGateway(AuthzToken authzToken, Gateway gateway) throws IamAdminServicesException, AuthorizationException {
    TenantManagementKeycloakImpl keycloakclient = new TenantManagementKeycloakImpl();
    PasswordCredential isSuperAdminCredentials = getSuperAdminPasswordCredential();
    try {
        keycloakclient.addTenant(isSuperAdminCredentials, gateway);
        // Load the tenant admin password stored in gateway request
        CredentialStoreService.Client credentialStoreClient = getCredentialStoreServiceClient();
        // Admin password token should already be stored under requested gateway's gatewayId
        PasswordCredential tenantAdminPasswordCredential = credentialStoreClient.getPasswordCredential(gateway.getIdentityServerPasswordToken(), gateway.getGatewayId());
        if (!keycloakclient.createTenantAdminAccount(isSuperAdminCredentials, gateway, tenantAdminPasswordCredential.getPassword())) {
            logger.error("Admin account creation failed !!, please refer error logs for reason");
        }
        Gateway gatewayWithIdAndSecret = keycloakclient.configureClient(isSuperAdminCredentials, gateway);
        return gatewayWithIdAndSecret;
    } catch (TException | ApplicationSettingsException ex) {
        logger.error("Gateway Setup Failed, reason: " + ex.getMessage(), ex);
        IamAdminServicesException iamAdminServicesException = new IamAdminServicesException(ex.getMessage());
        throw iamAdminServicesException;
    }
}
Also used : TenantManagementKeycloakImpl(org.apache.airavata.service.profile.iam.admin.services.core.impl.TenantManagementKeycloakImpl) TException(org.apache.thrift.TException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) Gateway(org.apache.airavata.model.workspace.Gateway) PasswordCredential(org.apache.airavata.model.credential.store.PasswordCredential) CredentialStoreService(org.apache.airavata.credential.store.cpi.CredentialStoreService) SecurityCheck(org.apache.airavata.service.security.interceptor.SecurityCheck)

Example 9 with Gateway

use of org.apache.airavata.model.workspace.Gateway in project airavata by apache.

the class TenantProfileServiceHandler method updateGateway.

@Override
@SecurityCheck
public boolean updateGateway(AuthzToken authzToken, Gateway updatedGateway) throws TenantProfileServiceException, AuthorizationException, TException {
    try {
        // if admin password token changes then copy the admin password and store under this gateway id and then update the admin password token
        Gateway existingGateway = tenantProfileRepository.getGateway(updatedGateway.getAiravataInternalGatewayId());
        if (updatedGateway.getIdentityServerPasswordToken() != null && (existingGateway.getIdentityServerPasswordToken() == null || !existingGateway.getIdentityServerPasswordToken().equals(updatedGateway.getIdentityServerPasswordToken()))) {
            copyAdminPasswordToGateway(authzToken, updatedGateway);
        }
        if (tenantProfileRepository.update(updatedGateway) != null) {
            logger.debug("Updated gateway-profile with ID: " + updatedGateway.getGatewayId());
            // replicate tenant at end-places
            ProfileServiceUtils.getDbEventPublisher().publish(ProfileServiceUtils.getDBEventMessageContext(EntityType.TENANT, CrudType.UPDATE, updatedGateway), DBEventManagerConstants.getRoutingKey(DBEventService.DB_EVENT.toString()));
            return true;
        } else {
            return false;
        }
    } catch (Exception ex) {
        logger.error("Error updating gateway-profile, reason: " + ex.getMessage(), ex);
        TenantProfileServiceException exception = new TenantProfileServiceException();
        exception.setMessage("Error updating gateway-profile, reason: " + ex.getMessage());
        return false;
    }
}
Also used : TenantProfileServiceException(org.apache.airavata.service.profile.tenant.cpi.exception.TenantProfileServiceException) Gateway(org.apache.airavata.model.workspace.Gateway) CredentialStoreException(org.apache.airavata.credential.store.exception.CredentialStoreException) TenantProfileServiceException(org.apache.airavata.service.profile.tenant.cpi.exception.TenantProfileServiceException) TException(org.apache.thrift.TException) AuthorizationException(org.apache.airavata.model.error.AuthorizationException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) SecurityCheck(org.apache.airavata.service.security.interceptor.SecurityCheck)

Example 10 with Gateway

use of org.apache.airavata.model.workspace.Gateway in project airavata by apache.

the class CreateLaunchExperiment method createGateway.

public static void createGateway() {
    try {
        Gateway gateway = new Gateway();
        gateway.setGatewayId("testGatewayId2");
        gateway.setGatewayName("testGateway2");
        gatewayId = airavataClient.addGateway(new AuthzToken(""), gateway);
        System.out.println(gatewayId);
    } catch (AiravataSystemException e) {
        e.printStackTrace();
    } catch (InvalidRequestException e) {
        e.printStackTrace();
    } catch (AiravataClientException e) {
        e.printStackTrace();
    } catch (TException e) {
        e.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) Gateway(org.apache.airavata.model.workspace.Gateway) AuthzToken(org.apache.airavata.model.security.AuthzToken)

Aggregations

Gateway (org.apache.airavata.model.workspace.Gateway)24 TException (org.apache.thrift.TException)13 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)8 AuthzToken (org.apache.airavata.model.security.AuthzToken)6 SecurityCheck (org.apache.airavata.service.security.interceptor.SecurityCheck)4 HashMap (java.util.HashMap)3 AiravataException (org.apache.airavata.common.exception.AiravataException)3 CredentialStoreException (org.apache.airavata.credential.store.exception.CredentialStoreException)3 GatewayResourceProfile (org.apache.airavata.model.appcatalog.gatewayprofile.GatewayResourceProfile)3 AiravataClientException (org.apache.airavata.model.error.AiravataClientException)3 InvalidRequestException (org.apache.airavata.model.error.InvalidRequestException)3 RegistryServiceException (org.apache.airavata.registry.api.exception.RegistryServiceException)3 Airavata (org.apache.airavata.api.Airavata)2 CredentialStoreException (org.apache.airavata.credential.store.store.CredentialStoreException)2 PasswordCredential (org.apache.airavata.model.credential.store.PasswordCredential)2 DBEventMessage (org.apache.airavata.model.dbevent.DBEventMessage)2 AiravataSystemException (org.apache.airavata.model.error.AiravataSystemException)2 AuthorizationException (org.apache.airavata.model.error.AuthorizationException)2 DuplicateEntryException (org.apache.airavata.model.error.DuplicateEntryException)2 UserProfile (org.apache.airavata.model.user.UserProfile)2