Search in sources :

Example 1 with FormKiqClientConnection

use of com.formkiq.stacks.client.FormKiqClientConnection in project formkiq-core by formkiq.

the class WebsocketTest method beforeClass.

/**
 * beforeclass.
 *
 * @throws IOException IOException
 * @throws URISyntaxException URISyntaxException
 */
@BeforeClass
public static void beforeClass() throws IOException, URISyntaxException {
    Region awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    String app = System.getProperty("testappenvironment");
    SqsConnectionBuilder sqsConnection = new SqsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    sqsService = new SqsService(sqsConnection);
    SsmConnectionBuilder ssmBuilder = new SsmConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    SsmService ssmService = new SsmServiceImpl(ssmBuilder);
    websocketSqsUrl = ssmService.getParameterValue("/formkiq/" + app + "/sqs/WebsocketUrl");
    websocketUrl = ssmService.getParameterValue("/formkiq/" + app + "/api/WebsocketUrl");
    String cognitoUserPoolId = ssmService.getParameterValue("/formkiq/" + app + "/cognito/UserPoolId");
    String cognitoClientId = ssmService.getParameterValue("/formkiq/" + app + "/cognito/UserPoolClientId");
    String cognitoIdentitypool = ssmService.getParameterValue("/formkiq/" + app + "/cognito/IdentityPoolId");
    CognitoConnectionBuilder adminBuilder = new CognitoConnectionBuilder(cognitoClientId, cognitoUserPoolId, cognitoIdentitypool).setCredentials(awsprofile).setRegion(awsregion);
    adminCognitoService = new CognitoService(adminBuilder);
    addAndLoginCognito(USER_EMAIL, GROUP);
    token = adminCognitoService.login(USER_EMAIL, USER_PASSWORD);
    String rootHttpUrl = ssmService.getParameterValue("/formkiq/" + app + "/api/DocumentsHttpUrl");
    FormKiqClientConnection connection = new FormKiqClientConnection(rootHttpUrl).cognitoIdToken(token.idToken()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
    httpClient = new FormKiqClientV1(connection);
    webconnectionsTable = ssmService.getParameterValue("/formkiq/" + app + "/dynamodb/WebConnectionsTableName");
    dbConnection = new DynamoDbConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
}
Also used : SsmService(com.formkiq.aws.ssm.SsmService) SsmServiceImpl(com.formkiq.aws.ssm.SsmServiceImpl) CognitoConnectionBuilder(com.formkiq.aws.cognito.CognitoConnectionBuilder) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) SsmConnectionBuilder(com.formkiq.aws.ssm.SsmConnectionBuilder) SqsService(com.formkiq.aws.sqs.SqsService) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) Region(software.amazon.awssdk.regions.Region) CognitoService(com.formkiq.aws.cognito.CognitoService) DynamoDbConnectionBuilder(com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) BeforeClass(org.junit.BeforeClass)

Example 2 with FormKiqClientConnection

use of com.formkiq.stacks.client.FormKiqClientConnection in project formkiq-core by formkiq.

the class DocumentsRequestTest method testGet02.

/**
 * Get Not existing file. Test user with no roles with/out siteid
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testGet02() throws Exception {
    // given
    final String siteId = "finance";
    AuthenticationResultType token = login(READONLY_EMAIL, USER_PASSWORD);
    FormKiqClientConnection connection = new FormKiqClientConnection(getRootHttpUrl()).cognitoIdToken(token.idToken()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
    FormKiqClientV1 client = new FormKiqClientV1(connection);
    GetDocumentsRequest request = new GetDocumentsRequest().date(new Date());
    // when
    final HttpResponse<String> responseNoSiteId = client.getDocumentsAsHttpResponse(request);
    final HttpResponse<String> responseSiteId = client.getDocumentsAsHttpResponse(request.siteId(siteId));
    // then
    assertEquals(STATUS_OK, responseNoSiteId.statusCode());
    assertRequestCorsHeaders(responseNoSiteId.headers());
    assertTrue(responseNoSiteId.body().contains("\"documents\":["));
    assertEquals(STATUS_FORBIDDEN, responseSiteId.statusCode());
    assertRequestCorsHeaders(responseSiteId.headers());
    assertEquals("{\"message\":\"Access Denied\"}", responseSiteId.body());
}
Also used : GetDocumentsRequest(com.formkiq.stacks.client.requests.GetDocumentsRequest) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) AuthenticationResultType(software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType) Date(java.util.Date) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 3 with FormKiqClientConnection

use of com.formkiq.stacks.client.FormKiqClientConnection in project formkiq-core by formkiq.

the class DocumentsRequestTest method testGet03.

/**
 * Get Not existing file. Test user with 'USERS' roles with/out siteid
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testGet03() throws Exception {
    // given
    final String siteId = "finance";
    AuthenticationResultType token = login(USER_EMAIL, USER_PASSWORD);
    FormKiqClientConnection connection = new FormKiqClientConnection(getRootHttpUrl()).cognitoIdToken(token.idToken()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
    FormKiqClientV1 client = new FormKiqClientV1(connection);
    GetDocumentsRequest request = new GetDocumentsRequest().date(new Date());
    // when
    final HttpResponse<String> responseNoSiteId = client.getDocumentsAsHttpResponse(request);
    final HttpResponse<String> responseSiteId = client.getDocumentsAsHttpResponse(request.siteId(siteId));
    // then
    assertEquals(STATUS_OK, responseNoSiteId.statusCode());
    assertRequestCorsHeaders(responseNoSiteId.headers());
    assertTrue(responseNoSiteId.body().contains("\"documents\":["));
    assertEquals(STATUS_FORBIDDEN, responseSiteId.statusCode());
    assertRequestCorsHeaders(responseSiteId.headers());
    assertEquals("{\"message\":\"Access Denied\"}", responseSiteId.body());
}
Also used : GetDocumentsRequest(com.formkiq.stacks.client.requests.GetDocumentsRequest) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) AuthenticationResultType(software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType) Date(java.util.Date) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 4 with FormKiqClientConnection

use of com.formkiq.stacks.client.FormKiqClientConnection in project formkiq-core by formkiq.

the class AbstractApiTest method setupCognito.

/**
 * Setup Cognito.
 */
private static void setupCognito() {
    if (!adminCognitoService.isUserExists(ADMIN_EMAIL)) {
        adminCognitoService.addUser(ADMIN_EMAIL, USER_TEMP_PASSWORD);
        adminCognitoService.addUserToGroup(ADMIN_EMAIL, "Admins");
        adminCognitoService.loginWithNewPassword(ADMIN_EMAIL, USER_TEMP_PASSWORD, USER_PASSWORD);
    } else {
        adminCognitoService.updateUserAttributes(ADMIN_EMAIL, Arrays.asList(AttributeType.builder().name("email_verified").value("true").build()));
        adminCognitoService.setUserPassword(ADMIN_EMAIL, USER_TEMP_PASSWORD, false);
        adminCognitoService.loginWithNewPassword(ADMIN_EMAIL, USER_TEMP_PASSWORD, USER_PASSWORD);
    }
    addAndLoginCognito(USER_EMAIL, DEFAULT_SITE_ID);
    addAndLoginCognito(FINANCE_EMAIL, "finance");
    addAndLoginCognito(READONLY_EMAIL, "default_read");
    adminToken = login(ADMIN_EMAIL, USER_PASSWORD);
    FormKiqClientConnection connection = new FormKiqClientConnection(rootHttpUrl).cognitoIdToken(adminToken.idToken()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
    httpClient = new FormKiqClientV1(connection);
}
Also used : FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1)

Example 5 with FormKiqClientConnection

use of com.formkiq.stacks.client.FormKiqClientConnection in project formkiq-core by formkiq.

the class AbstractApiTest method beforeClass.

/**
 * Before Class.
 *
 * @throws IOException IOException
 */
@BeforeClass
public static void beforeClass() throws IOException {
    awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    appenvironment = System.getProperty("testappenvironment");
    loadSsmParameterVariables(awsprofile);
    try (ProfileCredentialsProvider credentials = ProfileCredentialsProvider.builder().profileName(awsprofile).build()) {
        FormKiqClientConnection connection = new FormKiqClientConnection(rootRestUrl).region(awsregion).credentials(credentials.resolveCredentials()).header("Origin", Arrays.asList("http://localhost")).header("Access-Control-Request-Method", Arrays.asList("GET"));
        restClient = new FormKiqClientV1(connection);
    }
    CognitoConnectionBuilder adminBuilder = new CognitoConnectionBuilder(cognitoClientId, cognitoUserPoolId, cognitoIdentitypool).setCredentials(awsprofile).setRegion(awsregion);
    adminCognitoService = new CognitoService(adminBuilder);
    stsBuilder = new StsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    stsService = new StsService(stsBuilder);
    try (StsClient stsClient = stsBuilder.build()) {
        GetCallerIdentityResponse identity = stsClient.getCallerIdentity();
        String user = identity.arn().substring(identity.arn().lastIndexOf("/") + 1);
        IamConnectionBuilder iamBuilder = new IamConnectionBuilder().setCredentials(awsprofile);
        IamService iam = new IamService(iamBuilder);
        try (IamClient iamClient = iamBuilder.build()) {
            iam.addUserToGroup(iamClient, user, apiGatewayInvokeGroup);
        }
        executeApiSigner = new Aws4SignerParamsBuilder().setRegion(awsregion).setSigningName("execute-api").setCredentials(awsprofile).build();
    }
    setupCognito();
    setupConfigService(awsprofile);
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) IamConnectionBuilder(com.formkiq.aws.iam.IamConnectionBuilder) GetCallerIdentityResponse(software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse) StsConnectionBuilder(com.formkiq.aws.sts.StsConnectionBuilder) Aws4SignerParamsBuilder(com.formkiq.aws.sts.Aws4SignerParamsBuilder) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) CognitoConnectionBuilder(com.formkiq.aws.cognito.CognitoConnectionBuilder) IamClient(software.amazon.awssdk.services.iam.IamClient) StsService(com.formkiq.aws.sts.StsService) FormKiqClientConnection(com.formkiq.stacks.client.FormKiqClientConnection) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) IamService(com.formkiq.aws.iam.IamService) CognitoService(com.formkiq.aws.cognito.CognitoService) BeforeClass(org.junit.BeforeClass)

Aggregations

FormKiqClientConnection (com.formkiq.stacks.client.FormKiqClientConnection)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 CognitoConnectionBuilder (com.formkiq.aws.cognito.CognitoConnectionBuilder)2 CognitoService (com.formkiq.aws.cognito.CognitoService)2 GetDocumentsRequest (com.formkiq.stacks.client.requests.GetDocumentsRequest)2 Date (java.util.Date)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 AuthenticationResultType (software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType)2 IamConnectionBuilder (com.formkiq.aws.iam.IamConnectionBuilder)1 IamService (com.formkiq.aws.iam.IamService)1 SqsConnectionBuilder (com.formkiq.aws.sqs.SqsConnectionBuilder)1 SqsService (com.formkiq.aws.sqs.SqsService)1 SsmConnectionBuilder (com.formkiq.aws.ssm.SsmConnectionBuilder)1 SsmService (com.formkiq.aws.ssm.SsmService)1 SsmServiceImpl (com.formkiq.aws.ssm.SsmServiceImpl)1 Aws4SignerParamsBuilder (com.formkiq.aws.sts.Aws4SignerParamsBuilder)1 StsConnectionBuilder (com.formkiq.aws.sts.StsConnectionBuilder)1 StsService (com.formkiq.aws.sts.StsService)1 DynamoDbConnectionBuilder (com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder)1