From c3b1bb1a097854d0311d19d157c216859c435d43 Mon Sep 17 00:00:00 2001 From: Rucha Deshpande <deshruch@amazon.com> Date: Wed, 14 Oct 2020 19:42:24 +0000 Subject: [PATCH] AWS implementation of Notification Service commit 56d7e195 Author: Rucha Deshpande <deshruch@amazon.com> Date: Wed Oct 14 2020 10:57:36 GMT-0500 (Central Daylight Time) update env vars commit 6ef6c7fb Author: Rucha Deshpande <deshruch@amazon.com> Date: Wed Oct 14 2020 09:48:09 GMT-0500 (Central Daylight Time) Added AWS impl commit b6a6ddff Author: Rucha Deshpande <deshruch@amazon.com> Date: Fri Oct 02 2020 11:03:59 GMT-0500 (Central Daylight Time) Fix app properties commit 95c8ee63 Author: Rucha Deshpande <deshruch@amazon.com> Date: Fri Oct 02 2020 10:40:00 GMT-0500 (Central Daylight Time) Add base aws impl commit 79a7a2fb Author: Rucha Deshpande <deshruch@amazon.com> Date: Fri Oct 02 2020 07:29:17 GMT-0500 (Central Daylight Time) Add module notificationaws to reactor commit 75c315a8 Author: Rucha Deshpande <deshruch@amazon.com> Date: Fri Oct 02 2020 07:15:13 GMT-0500 (Central Daylight Time) Add base impl for build commit 544ea641 Author: Rucha Deshpande <deshruch@amazon.com> Date: Fri Oct 02 2020 06:58:57 GMT-0500 (Central Daylight Time) Setting up Build project --- pom.xml | 1 + .../notification-aws/build-aws/Dockerfile | 24 +++ .../notification-aws/build-aws/build-info.py | 88 +++++++++++ .../notification-aws/build-aws/buildspec.yaml | 84 +++++++++++ .../build-aws/os-notification.build.json | 83 +++++++++++ provider/notification-aws/maven/settings.xml | 76 ++++++++++ provider/notification-aws/pom.xml | 138 ++++++++++++++++++ .../provider/aws/Application.java | 31 ++++ .../provider/aws/impl/AwsAppProperties.java | 39 +++++ .../aws/impl/AwsGoogleServiceAccountImpl.java | 29 ++++ .../aws/impl/AwsPubSubHandshakeHandler.java | 24 +++ .../impl/AwsPubsubRequestBodyExtractor.java | 133 +++++++++++++++++ .../aws/impl/AwsServiceAccountValidator.java | 33 +++++ .../impl/ServiceAccountJwtAwsClientImpl.java | 15 ++ .../aws/security/AwsSecurityConfig.java | 33 +++++ .../src/main/resources/application.properties | 44 ++++++ .../build-aws/prepare-dist.sh | 47 ++++++ .../build-aws/run-tests.sh | 62 ++++++++ testing/notification-test-aws/pom.xml | 84 +++++++++++ .../api/TestPubsubEndpointHMAC.java | 40 +++++ .../notification/util/AwsCognitoClient.java | 93 ++++++++++++ .../osdu/notification/util/AwsConfig.java | 71 +++++++++ .../osdu/notification/util/AwsTestUtils.java | 57 ++++++++ .../api/PubsubEndpointHMACTests.java | 4 +- testing/pom.xml | 1 + 25 files changed, 1333 insertions(+), 1 deletion(-) create mode 100644 provider/notification-aws/build-aws/Dockerfile create mode 100644 provider/notification-aws/build-aws/build-info.py create mode 100644 provider/notification-aws/build-aws/buildspec.yaml create mode 100644 provider/notification-aws/build-aws/os-notification.build.json create mode 100644 provider/notification-aws/maven/settings.xml create mode 100644 provider/notification-aws/pom.xml create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/Application.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsAppProperties.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsGoogleServiceAccountImpl.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubSubHandshakeHandler.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsServiceAccountValidator.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/ServiceAccountJwtAwsClientImpl.java create mode 100644 provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/security/AwsSecurityConfig.java create mode 100644 provider/notification-aws/src/main/resources/application.properties create mode 100644 testing/notification-test-aws/build-aws/prepare-dist.sh create mode 100644 testing/notification-test-aws/build-aws/run-tests.sh create mode 100644 testing/notification-test-aws/pom.xml create mode 100644 testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java create mode 100644 testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsCognitoClient.java create mode 100644 testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsConfig.java create mode 100644 testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsTestUtils.java diff --git a/pom.xml b/pom.xml index 8bd5ba30b..1140ba250 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,7 @@ <module>provider/notification-gcp</module> <module>provider/notification-azure</module> <module>provider/notification-ibm</module> + <module>provider/notification-aws</module> </modules> <distributionManagement> diff --git a/provider/notification-aws/build-aws/Dockerfile b/provider/notification-aws/build-aws/Dockerfile new file mode 100644 index 000000000..c24b95cd8 --- /dev/null +++ b/provider/notification-aws/build-aws/Dockerfile @@ -0,0 +1,24 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html +FROM amazoncorretto:8 + +ARG JAR_FILE=provider/notification-aws/target/*spring-boot.jar +# Harcoding this value since Notification-core requires this variable. AWS does not use it. Might change in future +ENV ENVIRONMENT=DEV +WORKDIR / +COPY ${JAR_FILE} app.jar +EXPOSE 8080 +ENTRYPOINT java $JAVA_OPTS -jar /app.jar diff --git a/provider/notification-aws/build-aws/build-info.py b/provider/notification-aws/build-aws/build-info.py new file mode 100644 index 000000000..1ea9b54bd --- /dev/null +++ b/provider/notification-aws/build-aws/build-info.py @@ -0,0 +1,88 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import boto3 +import json +import os +import argparse + +# Create the build-info.json +parser = argparse.ArgumentParser(description="") + +# env - CODEBUILD_SOURCE_VERSION +parser.add_argument("--branch", type=str, help="") + +# env - CODEBUILD_RESOLVED_SOURCE_VERSION +parser.add_argument("--commit", type=str, help="") + +# env - CODEBUILD_BUILD_ID +parser.add_argument("--buildid", type=str, help="") + +# env - CODEBUILD_BUILD_NUMBER +parser.add_argument("--buildnumber", type=str, help="") + +# Get from directory name +parser.add_argument("--reponame", type=str, help="") + +# env OUTPUT_DIR +parser.add_argument("--outdir", type=str, help="") + +# full ecr image and tag, and any other artifacts +parser.add_argument("--artifact", type=str, action="append", help="") + + + +args = parser.parse_args() + +branch = args.branch +commitId = args.commit +buildId = args.buildid +buildNumber = args.buildnumber +repoName = args.reponame +outputDir = args.outdir +artifacts = args.artifact + +buildInfoFilePath = os.path.join(".", outputDir, "build-info.json") + +print(buildInfoFilePath) + +commitArgs = { + "repositoryName": repoName, + "commitId": commitId +} + +commitDetail = { + "commit": "" +} + +# get the commit detail +try: + codecommit = boto3.client("codecommit") + commitDetail = codecommit.get_commit(**commitArgs) +except Exception as e: + print("Getting commit information from codecommit failed") + +buildInfo = { + "branch": branch, + "build-id": buildId, + "build-number": buildNumber, + "repo": repoName, + "artifacts": artifacts, + "commit": commitDetail["commit"] +} +print(json.dumps(buildInfo, sort_keys=True, indent=4)) + +# write the build.json file to dist +f = open(buildInfoFilePath, "w") +f.write(json.dumps(buildInfo, sort_keys=True, indent=4)) +f.close() diff --git a/provider/notification-aws/build-aws/buildspec.yaml b/provider/notification-aws/build-aws/buildspec.yaml new file mode 100644 index 000000000..7fcbdfa04 --- /dev/null +++ b/provider/notification-aws/build-aws/buildspec.yaml @@ -0,0 +1,84 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html + +# https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html +version: 0.2 + +phases: + install: + runtime-versions: + java: corretto8 + commands: + - if [ $(echo $CODEBUILD_SOURCE_VERSION | grep -c ^refs/heads.*) -eq 1 ]; then echo "Branch name found"; else echo "This build only supports branch builds" && exit 1; fi + - apt-get update -y + - apt-get install -y maven + - java -version + - mvn -version + - mkdir -p /root/.m2 + - cp ./provider/notification-aws/maven/settings.xml /root/.m2/settings.xml # copy the AWS-specific settings.xml to the CodeBuild instance's .m2 folder + - export AWS_ACCOUNT_ID=`aws sts get-caller-identity | grep Account | cut -d':' -f 2 | cut -d'"' -f 2` + - export AWS_OSDU_DEV_MAVEN_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain $AWS_OSDU_DEV_MAVEN_DOMAIN --domain-owner $AWS_ACCOUNT_ID --query authorizationToken --output text` + pre_build: + commands: + - echo "Logging in to Amazon ECR..." + - $(aws ecr get-login --no-include-email --region $AWS_REGION) # authenticate with ECR via the AWS CLI + build: + commands: + - export REPO_NAME=${PWD##*/} + - export OUTPUT_DIR="dist" + - export BRANCH_NAME=`echo ${CODEBUILD_SOURCE_VERSION} | awk '{gsub("refs/heads/","");gsub("\\.","-");gsub("[[:space:]]","-")}1' | sed 's/\//-/g' | awk '{print tolower($0)}'` + - export ECR_TAG=`echo build.${BRANCH_NAME}.${CODEBUILD_BUILD_NUMBER}.${CODEBUILD_RESOLVED_SOURCE_VERSION} | cut -c 1-120` + - export ECR_IMAGE=${ECR_REGISTRY}:${ECR_TAG} + - export ECR_IMAGE_BRANCH_LATEST=${ECR_REGISTRY}:${BRANCH_NAME} + - export INTEGRATION_TEST_OUTPUT=${OUTPUT_DIR}/testing/integration + - export INTEGRATION_TEST_OUTPUT_BIN=${INTEGRATION_TEST_OUTPUT}/bin + - mkdir -p ${OUTPUT_DIR}/bin + - mkdir -p ${OUTPUT_DIR}/testing && mkdir -p ${INTEGRATION_TEST_OUTPUT} && mkdir -p ${INTEGRATION_TEST_OUTPUT}/bin + - echo "Placeholder" >> ${OUTPUT_DIR}/build-info.json # touched so that the output directory has some content incase the build fails so that testing reports are uploaded + - printenv + + - echo "Building primary service assemblies..." + - mvn -B test install -pl notification-core,provider/notification-aws -Ddeployment.environment=prod + + #- echo "Building integration testing assemblies and gathering artifacts..." + #- ./testing/notification-test-aws/build-aws/prepare-dist.sh + + - echo "Building docker image..." + - docker build -f provider/notification-aws/build-aws/Dockerfile -t ${ECR_IMAGE} . + - docker tag ${ECR_IMAGE} ${ECR_IMAGE_BRANCH_LATEST} + - echo "Pushing docker image..." + - docker push ${ECR_IMAGE} + - docker push ${ECR_IMAGE_BRANCH_LATEST} + + - echo "Generate build-info.json" + - | + python provider/notification-aws/build-aws/build-info.py --branch ${CODEBUILD_SOURCE_VERSION} --commit ${CODEBUILD_RESOLVED_SOURCE_VERSION} \ + --buildid ${CODEBUILD_BUILD_ID} --buildnumber ${CODEBUILD_BUILD_NUMBER} --reponame ${REPO_NAME} --outdir ${OUTPUT_DIR} \ + --artifact ${ECR_IMAGE} +reports: + SurefireReports: # CodeBuild will create a report group called "SurefireReports". + files: #Store all of the files + - "notification-core/target/surefire-reports/**/*" + - "provider/notification-aws/target/surefire-reports/**/*" + base-directory: "." # Location of the reports +artifacts: + files: + - "**/*" + base-directory: "dist" + name: ${REPO_NAME}_${BRANCH_NAME}_$(date +%F)_${CODEBUILD_BUILD_NUMBER}.zip +cache: + paths: + - "/root/.m2/**/*" \ No newline at end of file diff --git a/provider/notification-aws/build-aws/os-notification.build.json b/provider/notification-aws/build-aws/os-notification.build.json new file mode 100644 index 000000000..3cd8fc8a4 --- /dev/null +++ b/provider/notification-aws/build-aws/os-notification.build.json @@ -0,0 +1,83 @@ +{ + "name": "os-notification", + "description": "Build of the os-notification repository", + "source": { + "type": "CODECOMMIT", + "location": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/os-notification", + "gitCloneDepth": 1, + "gitSubmodulesConfig": { + "fetchSubmodules": false + }, + "buildspec": "./provider/notification-aws/build-aws/buildspec.yaml", + "insecureSsl": false + }, + "secondarySources": [], + "sourceVersion": "refs/heads/dev", + "secondarySourceVersions": [], + "artifacts": { + "type": "S3", + "location": "888733619319-devops-build-artifacts", + "path": "os-notification", + "namespaceType": "NONE", + "name": "os-notification", + "packaging": "ZIP", + "overrideArtifactName": true, + "encryptionDisabled": false + }, + "secondaryArtifacts": [], + "cache": { + "type": "LOCAL", + "modes": [ + "LOCAL_CUSTOM_CACHE" + ] + }, + "environment": { + "type": "LINUX_CONTAINER", + "image": "aws/codebuild/standard:4.0", + "computeType": "BUILD_GENERAL1_SMALL", + "environmentVariables": [ + { + "name": "ECR_REGISTRY", + "value": "888733619319.dkr.ecr.us-east-1.amazonaws.com/os-notification_dev", + "type": "PLAINTEXT" + }, + { + "name": "AWS_OSDU_DEV_MAVEN_URL", + "value": "https://osdu-dev-888733619319.d.codeartifact.us-east-1.amazonaws.com/maven/osdu-maven/", + "type": "PLAINTEXT" + }, + { + "name": "AWS_OSDU_DEV_MAVEN_DOMAIN", + "value": "osdu-dev", + "type": "PLAINTEXT" + } + ], + "privilegedMode": true, + "imagePullCredentialsType": "CODEBUILD" + }, + "serviceRole": "arn:aws:iam::888733619319:role/service-role/dev-CodeBuildRole", + "timeoutInMinutes": 60, + "queuedTimeoutInMinutes": 480, + "encryptionKey": "arn:aws:kms:us-east-1:888733619319:alias/aws/s3", + "tags": [], + "vpcConfig": { + "vpcId": "vpc-0f273733df61bc541", + "subnets": [ + "subnet-03963a50e77043e12", + "subnet-04a975f0e6e0c9279" + ], + "securityGroupIds": [ + "sg-0dee4e811c2062e26" + ] + }, + "badgeEnabled": true, + "logsConfig": { + "cloudWatchLogs": { + "status": "ENABLED" + }, + "s3Logs": { + "status": "DISABLED", + "encryptionDisabled": false + } + } +} diff --git a/provider/notification-aws/maven/settings.xml b/provider/notification-aws/maven/settings.xml new file mode 100644 index 000000000..b8192246c --- /dev/null +++ b/provider/notification-aws/maven/settings.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright © 2020 Amazon Web Services + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> + + <profiles> + <profile> + <id>aws-osdu-dev-maven</id> + <activation> + <activeByDefault>true</activeByDefault> + </activation> + <repositories> + <repository> + <id>aws-osdu-dev-maven</id> + <url>${env.AWS_OSDU_DEV_MAVEN_URL}</url> + </repository> + <repository> + <id>gitlab-os-core-common-maven</id> + <url>https://community.opengroup.org/api/v4/projects/67/packages/maven</url> + </repository> + </repositories> + </profile> + <profile> + <id>credentialsConfiguration</id> + <activation> + <activeByDefault>true</activeByDefault> + </activation> + <properties> + <deployment.environment>dev</deployment.environment> + <aws.accessKeyId>no-default</aws.accessKeyId> + <aws.secretKey>no-default</aws.secretKey> + <azure.devops.username>Another-Access-Token-2021</azure.devops.username> + <azure.devops.token>no-default</azure.devops.token> + </properties> + </profile> + </profiles> + + <servers> + <server> + <id>aws-osdu-dev-maven</id> + <username>aws</username> + <password>${env.AWS_OSDU_DEV_MAVEN_AUTH_TOKEN}</password> + </server> + </servers> + + <!-- CodeArtifact doesn't support external repos yet that aren't Maven Central. ETA Q4 2020. --> + <!-- <mirrors> --> + <!-- <mirror> --> + <!-- <id>aws-osdu-dev-maven</id> --> + <!-- <name>aws-osdu-dev-maven</name> --> + <!-- <url>https://osdu-dev-888733619319.d.codeartifact.us-east-1.amazonaws.com/maven/osdu-maven/</url> --> + <!-- <mirrorOf>*,!gitlab-os-core-common-maven</mirrorOf> --> + <!-- </mirror> --> + <!-- </mirrors> --> + + <activeProfiles> + <activeProfile>credentialsConfiguration</activeProfile> + </activeProfiles> + +</settings> \ No newline at end of file diff --git a/provider/notification-aws/pom.xml b/provider/notification-aws/pom.xml new file mode 100644 index 000000000..19be32af5 --- /dev/null +++ b/provider/notification-aws/pom.xml @@ -0,0 +1,138 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright © 2020 Amazon Web Services + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.opengroup.osdu</groupId> + <artifactId>notification-aws</artifactId> + <version>1.0.0</version> + <name>notification-aws</name> + <description>AWS implementation for Notification service</description> + <packaging>jar</packaging> + + <parent> + <groupId>org.opengroup.osdu</groupId> + <artifactId>os-notification</artifactId> + <version>1.0.0</version> + <relativePath>../../pom.xml</relativePath> + </parent> + + <properties> + <java.version>8</java.version> + <maven.compiler.target>${java.version}</maven.compiler.target> + <maven.compiler.source>${java.version}</maven.compiler.source> + </properties> + + <repositories> + <repository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/groups/17/-/packages/maven</url> + </repository> + </repositories> + + <distributionManagement> + <repository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/projects/143/packages/maven</url> + </repository> + <snapshotRepository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/projects/143/packages/maven</url> + </snapshotRepository> + </distributionManagement> + + <dependencies> + <!-- <dependency> + <groupId>org.opengroup.osdu</groupId> + <artifactId>os-core-common</artifactId> + </dependency> --> + + + <dependency> + <groupId>org.opengroup.osdu.core.aws</groupId> + <artifactId>os-core-lib-aws</artifactId> + <version>0.3.11-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.opengroup.osdu</groupId> + <artifactId>notification-core</artifactId> + <version>1.0.0</version> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + + <!-- unit test dependencies --> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito2</artifactId> + <version>2.0.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>2.0.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>2.0.2-beta</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + <configuration> + <classifier>spring-boot</classifier> + <mainClass> + org.opengroup.osdu.notification.provider.aws.Application + </mainClass> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/Application.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/Application.java new file mode 100644 index 000000000..c40dadeba --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/Application.java @@ -0,0 +1,31 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +package org.opengroup.osdu.notification.provider.aws; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@ComponentScan({"org.opengroup.osdu"}) +@EnableAsync +public class Application { + + public static void main(String[] args) { + SpringApplication.run(new Class[]{Application.class}, args); + } +} + diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsAppProperties.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsAppProperties.java new file mode 100644 index 000000000..b93d10a7a --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsAppProperties.java @@ -0,0 +1,39 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package org.opengroup.osdu.notification.provider.aws.impl; + + +import org.opengroup.osdu.notification.provider.interfaces.IAppProperties; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class AwsAppProperties implements IAppProperties { + + @Value("${AUTHORIZE_API}") + private String authorizeAPI; + + @Value("${REGISTER_SERVICE_URL}") + private String registerAPI; + + @Override + public String getAuthorizeAPI() { + return authorizeAPI; + } + + @Override + public String getRegisterAPI() { + return registerAPI; + } +} \ No newline at end of file diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsGoogleServiceAccountImpl.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsGoogleServiceAccountImpl.java new file mode 100644 index 000000000..6a47ba1c7 --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsGoogleServiceAccountImpl.java @@ -0,0 +1,29 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package org.opengroup.osdu.notification.provider.aws.impl; + +import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; +import org.springframework.stereotype.Component; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +@Component +public class AwsGoogleServiceAccountImpl implements IGoogleServiceAccount { + @Override + public String getIdToken(String keyString, String audience) { + // TODO : Check if it is to be supported + + throw new NotImplementedException(); + } +} + + diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubSubHandshakeHandler.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubSubHandshakeHandler.java new file mode 100644 index 000000000..eb0842a07 --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubSubHandshakeHandler.java @@ -0,0 +1,24 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package org.opengroup.osdu.notification.provider.aws.impl; + +import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler; +import org.springframework.stereotype.Component; + +@Component +public class AwsPubSubHandshakeHandler implements IPubsubHandshakeHandler { + @Override + public String getHandshakeResponse() { + return null; + } +} diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java new file mode 100644 index 000000000..865854554 --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java @@ -0,0 +1,133 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package org.opengroup.osdu.notification.provider.aws.impl; + +import com.google.common.base.Strings; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.model.storage.MessageContent; +import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Component +@RequestScope +public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtractor { + private static final String INVALID_PUBSUB_MESSAGE = "Invalid Publish-Subscribe Message format "; + private static final Gson GSON = new Gson(); + private MessageContent messageContent; + private JsonObject root = null; + + @Autowired + private HttpServletRequest request; + + @Autowired + private JaxRsDpsLog log; + public Map<String, String> extractAttributesFromRequestBody() { + if (this.messageContent == null) { + this.messageContent = this.extractPubsubMessageFromRequestBody(); + } + return this.messageContent.getAttributes(); + } + + public String extractDataFromRequestBody() { + if (this.messageContent == null) { + this.messageContent = this.extractPubsubMessageFromRequestBody(); + } + return this.messageContent.getData(); + } + + public String extractNotificationIdFromRequestBody() { + if (this.root == null) { + this.root = this.extractRootJsonElementFromRequestBody(); + } + JsonElement subscription = this.root.get("subscription"); + if (subscription == null) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Subscription object not found"); + } + + String[] fullNotificationId = subscription.getAsString().split("/"); + return fullNotificationId[fullNotificationId.length - 1]; + } + + @Override + public boolean isHandshakeRequest() { + return false; + } + + private MessageContent extractPubsubMessageFromRequestBody() { + if (this.root == null) { + this.root = this.extractRootJsonElementFromRequestBody(); + } + JsonElement message = this.root.get("message"); + if (message == null) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Message object not found"); + } + MessageContent content = GSON.fromJson(message.toString(), MessageContent.class); + + Map<String, String> attributes = content.getAttributes(); + if (attributes == null || attributes.isEmpty()) { + log.error("Incorrect Message: " + message.toString() ); + throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Attribute map not found"); + } + String data = content.getData(); + if (Strings.isNullOrEmpty(data)) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Data field not found"); + } + Map<String, String> lowerCase = new HashMap<>(); + attributes.forEach((key, value) -> lowerCase.put(key.toLowerCase(), value)); + if (Strings.isNullOrEmpty(attributes.get("data-partition-id"))) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, + "No tenant information from pubsub message."); + } + content.setAttributes(lowerCase); + + String decoded = new String(Base64.getDecoder().decode(data)); + content.setData(decoded); + + return content; + } + + private JsonObject extractRootJsonElementFromRequestBody() { + try { + JsonParser jsonParser = new JsonParser(); + BufferedReader reader = request.getReader(); + Stream<String> lines = reader.lines(); + String requestBody = lines.collect(Collectors.joining("\n")); + JsonElement rootElement = jsonParser.parse(requestBody); + if (!(rootElement instanceof JsonObject)) { + throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.", + "Request Body should be JsonObject to be processed."); + } + return rootElement.getAsJsonObject(); + } catch (IOException e) { + throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error", + "Unable to parse request payload.", e); + } + } +} \ No newline at end of file diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsServiceAccountValidator.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsServiceAccountValidator.java new file mode 100644 index 000000000..5e30acebf --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsServiceAccountValidator.java @@ -0,0 +1,33 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package org.opengroup.osdu.notification.provider.aws.impl; + +import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator; +import org.springframework.stereotype.Service; + +@Service +public class AwsServiceAccountValidator implements IServiceAccountValidator { + @Override + public boolean isValidPublisherServiceAccount(String jwt) { + //Do we need to implement this since authz is done through Entitlements? + //Keeping false since we want to "Default Deny" + return false; + } + + @Override + public boolean isValidServiceAccount(String jwt, String userIdentity, String... audiences) { + //Do we need to implement this since authz is done through Entitlements? + //Keeping false since we want to "Default Deny" + return false; + } +} diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/ServiceAccountJwtAwsClientImpl.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/ServiceAccountJwtAwsClientImpl.java new file mode 100644 index 000000000..0c96df5d3 --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/ServiceAccountJwtAwsClientImpl.java @@ -0,0 +1,15 @@ +package org.opengroup.osdu.notification.provider.aws.impl; + +import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient; +import org.springframework.stereotype.Component; + +@Component +public class ServiceAccountJwtAwsClientImpl implements IServiceAccountJwtClient { + @Override + public String getIdToken(String s) { + + String token ="not used"; + return token; + + } +} diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/security/AwsSecurityConfig.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/security/AwsSecurityConfig.java new file mode 100644 index 000000000..d683b65ca --- /dev/null +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/security/AwsSecurityConfig.java @@ -0,0 +1,33 @@ +// Copyright © 2020 Amazon Web Services +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.provider.aws.security; + + +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + + +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class AwsSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http.httpBasic().disable() + .csrf().disable(); //disable default authN. AuthN handled by endpoints proxy + } +} + + diff --git a/provider/notification-aws/src/main/resources/application.properties b/provider/notification-aws/src/main/resources/application.properties new file mode 100644 index 000000000..627c87d6d --- /dev/null +++ b/provider/notification-aws/src/main/resources/application.properties @@ -0,0 +1,44 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOG_PREFIX=notification +logging.level.org.springframework.web=${LOG_LEVEL:INFO} +server.servlet.contextPath=/api/notification/v1 +server.port=${APPLICATION_PORT:8080} + +AUTHORIZE_API=${ENTITLEMENTS_BASE_URL}/api/entitlements/v1 + +REGISTER_SERVICE_URL=${REGISTER_BASE_URL}/api/register/v1 + +## AWS DynamoDB configuration +aws.region=${AWS_REGION} +aws.dynamodb.table.prefix=${RESOURCE_PREFIX}- +aws.dynamodb.endpoint=dynamodb.${AWS_REGION}.amazonaws.com + + +app.expireTime=300 +app.maxCacheSize=10 + + +# Application name +spring.application.name=notification-aws + + +#logging configuration +logging.transaction.enabled=true +logging.slf4jlogger.enabled=true +logging.mdccontext.enabled=true + +# if this is turned on then the service tries to connect to elastic search +management.health.elasticsearch.enabled=false diff --git a/testing/notification-test-aws/build-aws/prepare-dist.sh b/testing/notification-test-aws/build-aws/prepare-dist.sh new file mode 100644 index 000000000..4901fec99 --- /dev/null +++ b/testing/notification-test-aws/build-aws/prepare-dist.sh @@ -0,0 +1,47 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script prepares the dist directory for the integration tests. +# Must be run from the root of the repostiory + +# This script prepares the dist directory for the integration tests. +# Must be run from the root of the repostiory + +set -e + +OUTPUT_DIR="${OUTPUT_DIR:-dist}" + +INTEGRATION_TEST_OUTPUT_DIR=${INTEGRATION_TEST_OUTPUT_DIR:-$OUTPUT_DIR}/testing/integration +INTEGRATION_TEST_OUTPUT_BIN_DIR=${INTEGRATION_TEST_OUTPUT_DIR:-$INTEGRATION_TEST_OUTPUT_DIR}/bin +INTEGRATION_TEST_SOURCE_DIR=testing +INTEGRATION_TEST_SOURCE_DIR_AWS="$INTEGRATION_TEST_SOURCE_DIR"/notification-test-aws +INTEGRATION_TEST_SOURCE_DIR_CORE="$INTEGRATION_TEST_SOURCE_DIR"/notification-test-core +echo "--Source directories variables--" +echo $INTEGRATION_TEST_SOURCE_DIR_AWS +echo $INTEGRATION_TEST_SOURCE_DIR_CORE +echo "--Output directories variables--" +echo $OUTPUT_DIR +echo $INTEGRATION_TEST_OUTPUT_DIR +echo $INTEGRATION_TEST_OUTPUT_BIN_DIR + +rm -rf "$INTEGRATION_TEST_OUTPUT_DIR" +mkdir -p "$INTEGRATION_TEST_OUTPUT_DIR" && mkdir -p "$INTEGRATION_TEST_OUTPUT_BIN_DIR" +echo "Building integration testing assemblies and gathering artifacts..." +mvn install -f "$INTEGRATION_TEST_SOURCE_DIR_CORE"/pom.xml +mvn install dependency:copy-dependencies -DskipTests -f "$INTEGRATION_TEST_SOURCE_DIR_AWS"/pom.xml -DincludeGroupIds=org.opengroup.osdu -Dmdep.copyPom +cp "$INTEGRATION_TEST_SOURCE_DIR_AWS"/target/dependency/* "${INTEGRATION_TEST_OUTPUT_BIN_DIR}" +(cd "${INTEGRATION_TEST_OUTPUT_BIN_DIR}" && ls *.jar | sed -e 's/\.jar$//' | xargs -I {} echo mvn install:install-file -Dfile={}.jar -DpomFile={}.pom >> install-deps.sh) +chmod +x "${INTEGRATION_TEST_OUTPUT_BIN_DIR}"/install-deps.sh +mvn clean -f "$INTEGRATION_TEST_SOURCE_DIR_AWS"/pom.xml +cp -R "$INTEGRATION_TEST_SOURCE_DIR_AWS"/* "${INTEGRATION_TEST_OUTPUT_DIR}"/ \ No newline at end of file diff --git a/testing/notification-test-aws/build-aws/run-tests.sh b/testing/notification-test-aws/build-aws/run-tests.sh new file mode 100644 index 000000000..973539f7a --- /dev/null +++ b/testing/notification-test-aws/build-aws/run-tests.sh @@ -0,0 +1,62 @@ +# Copyright © 2020 Amazon Web Services +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script prepares the dist directory for the integration tests. +# Must be run from the root of the repostiory + +# This script executes the test and copies reports to the provided output directory +# To call this script from the service working directory +# ./dist/testing/integration/build-aws/run-tests.sh "./reports/" + + +echo "### Running Notification-Service Integration Tests... ###" +SCRIPT_SOURCE_DIR=$(dirname "$0") +echo "Script source location" +echo "$SCRIPT_SOURCE_DIR" +(cd "$SCRIPT_SOURCE_DIR"/../bin && ./install-deps.sh) + +#### ADD REQUIRED ENVIRONMENT VARIABLES HERE ############################################### +# The following variables are automatically populated from the environment during integration testing +# see os-deploy-aws/build-aws/integration-test-env-variables.py for an updated list + + +export AWS_COGNITO_AUTH_FLOW=USER_PASSWORD_AUTH +export AWS_COGNITO_AUTH_PARAMS_PASSWORD=$ADMIN_PASSWORD +export AWS_COGNITO_AUTH_PARAMS_USER=$ADMIN_USER +export AWS_COGNITO_AUTH_PARAMS_USER_NO_ACCESS=$USER_NO_ACCESS +export ENVIRONMENT=DEV +export REGISTER_BASE_URL=$REGISTER_BASE_URL +export HMAC_SECRET=02030405060708090A0B0C0D0E0F +export REGISTER_CUSTOM_PUSH_PATH=api/register/v1/awstest/aws/challenge +export REGISTER_CUSTOM_PUSH_PATH1=api/register/v1/awstest/aws/challenge/1 +export REGISTER_CUSTOM_PUSH_URL=$REGISTER_BASE_URL$REGISTER_CUSTOM_PUSH_PATH +export REGISTER_CUSTOM_PUSH_URL1=$REGISTER_BASE_URL$REGISTER_CUSTOM_PUSH_PATH1 + + + + +#### RUN INTEGRATION TEST ######################################################################### + +mvn test -f "$SCRIPT_SOURCE_DIR"/../pom.xml +TEST_EXIT_CODE=$? + +#### COPY TEST REPORTS ######################################################################### + +if [ -n "$1" ] + then + mkdir -p "$1" + cp -R "$SCRIPT_SOURCE_DIR"/../target/surefire-reports "$1" +fi +echo "### Notification-Service Integration Tests Finished ###" +exit $TEST_EXIT_CODE \ No newline at end of file diff --git a/testing/notification-test-aws/pom.xml b/testing/notification-test-aws/pom.xml new file mode 100644 index 000000000..4bcc8aa3c --- /dev/null +++ b/testing/notification-test-aws/pom.xml @@ -0,0 +1,84 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright © 2020 Amazon Web Services + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.opengroup.osdu</groupId> + <artifactId>notification-test-aws</artifactId> + <version>1.0-SNAPSHOT</version> + <name>notification-test-aws</name> + <description>AWS Integration tests for Notification Service</description> + <packaging>jar</packaging> + + <properties> + <java.version>8</java.version> + <maven.compiler.target>${java.version}</maven.compiler.target> + <maven.compiler.source>${java.version}</maven.compiler.source> + <os-core-lib-aws.version>0.3.11-SNAPSHOT</os-core-lib-aws.version> + </properties> + + <repositories> + <repository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/groups/17/-/packages/maven</url> + </repository> + </repositories> + + <distributionManagement> + <repository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/projects/143/packages/maven</url> + </repository> + <snapshotRepository> + <id>${gitlab-server}</id> + <url>https://community.opengroup.org/api/v4/projects/143/packages/maven</url> + </snapshotRepository> + </distributionManagement> + + <dependencies> + <dependency> + <groupId>org.opengroup.osdu.core.aws</groupId> + <artifactId>os-core-lib-aws</artifactId> + <version>0.3.11-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>com.amazonaws</groupId> + <artifactId>aws-java-sdk-cognitoidp</artifactId> + <version>1.11.676</version> + </dependency> + <dependency> + <groupId>org.opengroup.osdu.notification</groupId> + <artifactId>notification-test-core</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.opengroup.osdu</groupId> + <artifactId>os-core-common</artifactId> + <version>0.3.6</version> + </dependency> + + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + </dependency> + </dependencies> + +</project> diff --git a/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java new file mode 100644 index 000000000..caeaec919 --- /dev/null +++ b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java @@ -0,0 +1,40 @@ +package org.opengroup.osdu.notification.api; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.opengroup.osdu.notification.util.Config; +import org.opengroup.osdu.notification.util.AwsTestUtils; +import org.opengroup.osdu.notification.util.RestDescriptor; +import org.opengroup.osdu.notification.util.TestUtils; + +import com.sun.jersey.api.client.ClientResponse; + +public class TestPubsubEndpointHMAC extends PubsubEndpointHMACTests { + + @BeforeClass + public static void classSetup() throws Exception { + PubsubEndpointHMACTests.classSetup(); + } + + @AfterClass + public static void classTearDown() throws Exception { + } + + @Before + @Override + public void setup() throws Exception { + this.testUtils = new AwsTestUtils(); + } + + @After + @Override + public void tearDown() throws Exception { + this.testUtils = null; + } + + +} \ No newline at end of file diff --git a/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsCognitoClient.java b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsCognitoClient.java new file mode 100644 index 000000000..e63144347 --- /dev/null +++ b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsCognitoClient.java @@ -0,0 +1,93 @@ +// Copyright © 2020 Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.util; + +import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider; +import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder; +import com.amazonaws.services.cognitoidp.model.InitiateAuthRequest; +import com.amazonaws.services.cognitoidp.model.InitiateAuthResult; + +import org.opengroup.osdu.core.aws.iam.IAMConfig; + +import java.util.HashMap; +import java.util.Map; + +public class AwsCognitoClient { + + // Parameter value locations + private final static String USERNAME_PARAM = "USERNAME"; + private final static String PASSWORD_PARAM = "PASSWORD"; + private final static String COGNITO_CLIENT_ID_PROPERTY = "AWS_COGNITO_CLIENT_ID"; + private final static String COGNITO_AUTH_FLOW_PROPERTY = "AWS_COGNITO_AUTH_FLOW"; + private final static String COGNITO_AUTH_PARAMS_USER_PROPERTY = "AWS_COGNITO_AUTH_PARAMS_USER"; + private final static String COGNITO_AUTH_PARAMS_PASSWORD_PROPERTY = "AWS_COGNITO_AUTH_PARAMS_PASSWORD"; + + + String awsCognitoClientId; + String awsCognitoAuthFlow; + String awsCognitoAuthParamsUser; + String awsCognitoAuthParamsPassword; + AWSCognitoIdentityProvider provider; + + public AwsCognitoClient() { + this.awsCognitoClientId = AwsConfig.getAWSCognitoClientId(); + this.awsCognitoAuthFlow = AwsConfig.getAWSCognitoAuthFlow(); + this.awsCognitoAuthParamsUser = AwsConfig.getAWSCognitoUser(); + this.awsCognitoAuthParamsPassword = AwsConfig.getAWSCognitoPassword(); + this.provider =generateCognitoClient(); + } + + public AwsCognitoClient(String awsCognitoClientId, String awsCognitoAuthFlow, String awsCognitoAuthParamsUser + , String awsCognitoAuthParamsPassword) { + this.awsCognitoClientId = awsCognitoClientId; + this.awsCognitoAuthFlow = awsCognitoAuthFlow; + this.awsCognitoAuthParamsUser = awsCognitoAuthParamsUser; + this.awsCognitoAuthParamsPassword = awsCognitoAuthParamsPassword; + this.provider = generateCognitoClient(); + + + } + + public String getToken(String username, String password,String tokenType){ + Map<String, String> authParameters = new HashMap<>(); + authParameters.put(USERNAME_PARAM, username); + authParameters.put(PASSWORD_PARAM, password); + + InitiateAuthRequest request = new InitiateAuthRequest(); + request.setClientId(awsCognitoClientId); + request.setAuthFlow(awsCognitoAuthFlow); + request.setAuthParameters(authParameters); + String token=""; + InitiateAuthResult result = this.provider.initiateAuth(request); + if(tokenType.equals("session")) + token= result.getSession(); + else if(tokenType.equals("bearer")) + token= "Bearer "+ result.getAuthenticationResult().getAccessToken(); + return token; + } + + + public AWSCognitoIdentityProvider getProvider() { + return provider; + } + + public static AWSCognitoIdentityProvider generateCognitoClient() + { + return AWSCognitoIdentityProviderClientBuilder.standard() + .withCredentials(IAMConfig.amazonAWSCredentials()) + .withRegion(AwsConfig.getAwsRegion()) + .build(); + } +} diff --git a/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsConfig.java b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsConfig.java new file mode 100644 index 000000000..daee4b6a5 --- /dev/null +++ b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsConfig.java @@ -0,0 +1,71 @@ +// Copyright © 2020 Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.util; + + +public class AwsConfig { + + + + public static String getAWSCognitoClientId() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_CLIENT_ID", ""); + } + + public static String getAWSCognitoAuthFlow() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_AUTH_FLOW", "USER_PASSWORD_AUTH"); + } + + public static String getAWSCognitoUser() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_AUTH_PARAMS_USER", ""); + } + + public static String getAWSCognitoPassword() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_AUTH_PARAMS_PASSWORD", ""); + } + + public static String getAWSCognitoNoAccessUser() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_AUTH_PARAMS_USER_NO_ACCESS", ""); + } + + + public static String getAWSCognitoUserPoolID() { + return getEnvironmentVariableOrDefaultValue("AWS_COGNITO_USER_POOL_ID", ""); + } + + public static String getResourcePrefix() { + return getEnvironmentVariableOrDefaultValue("RESOURCE_PREFIX", ""); + } + + public static String getAwsRegion() { + return getEnvironmentVariableOrDefaultValue("AWS_REGION", "us-east-1"); + } + + + + private static String getEnvironmentVariableOrDefaultValue(String key, String defaultValue) { + String environmentVariable = getEnvironmentVariable(key); + if (environmentVariable == null) { + environmentVariable = defaultValue; + } + return environmentVariable; + } + + private static String getEnvironmentVariable(String propertyKey) { + return System.getProperty(propertyKey, System.getenv(propertyKey)); + } + + + +} diff --git a/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsTestUtils.java b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsTestUtils.java new file mode 100644 index 000000000..9b0bb8874 --- /dev/null +++ b/testing/notification-test-aws/src/test/java/org/opengroup/osdu/notification/util/AwsTestUtils.java @@ -0,0 +1,57 @@ +// Copyright © 2020 Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.notification.util; + + +import org.apache.commons.lang3.StringUtils; + +public class AwsTestUtils extends TestUtils{ + + + + AwsCognitoClient client = new AwsCognitoClient(); + + @Override + public String getOpsToken() throws Exception { + if (opsToken==null || StringUtils.isEmpty(opsToken) ) { + opsToken= client.getToken(AwsConfig.getAWSCognitoUser(),AwsConfig.getAWSCognitoPassword(),"bearer"); + } + return opsToken; + } + + @Override + public String getAdminToken() throws Exception { + if (adminToken==null || StringUtils.isEmpty(adminToken)) { + adminToken=client.getToken(AwsConfig.getAWSCognitoUser(),AwsConfig.getAWSCognitoPassword(),"bearer"); + } + return adminToken; + } + + @Override + public String getEditorToken() throws Exception { + if (editorToken==null || StringUtils.isEmpty(editorToken)) { + editorToken=client.getToken(AwsConfig.getAWSCognitoUser(),AwsConfig.getAWSCognitoPassword(),"bearer"); + } + return editorToken; + } + + @Override + public String getNoAccessToken() throws Exception { + if (noAccessToken==null || StringUtils.isEmpty(noAccessToken)) { + noAccessToken=client.getToken(AwsConfig.getAWSCognitoNoAccessUser(),AwsConfig.getAWSCognitoPassword(),"bearer"); + } + return noAccessToken; + } +} diff --git a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACTests.java b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACTests.java index 200944651..11b88bffa 100644 --- a/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACTests.java +++ b/testing/notification-test-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpointHMACTests.java @@ -84,7 +84,9 @@ public abstract class PubsubEndpointHMACTests extends BaseTestTemplate { subscription.setName("subscription-integration-test-hmac"); subscription.setDescription("subscription created for hmac integration test"); subscription.setTopic(Config.Instance().Topic); - subscription.setPushEndpoint(Config.Instance().HMACPushUrl + "hmac-integration-test"); + //This seems to be a bug + //subscription.setPushEndpoint(Config.Instance().HMACPushUrl + "hmac-integration-test"); + subscription.setPushEndpoint(Config.Instance().HMACPushUrl); HmacSecret secret = new HmacSecret(); secret.setValue(Config.Instance().hmacSecretValue); diff --git a/testing/pom.xml b/testing/pom.xml index 98ce82767..021529541 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -35,6 +35,7 @@ <module>notification-test-core</module> <module>notification-test-azure</module> <module>notification-test-gcp</module> + <module>notification-test-aws</module> </modules> <repositories> -- GitLab