From 7dd83808236189bcdd93e780fec342cccc5788ce Mon Sep 17 00:00:00 2001 From: Andre Wlodkovski Date: Thu, 28 Mar 2024 20:38:27 -0300 Subject: [PATCH 1/4] Change database engine to PostgresQL --- .../20240227173707_auth_test/migration.sql | 12 --------- .../migration.sql | 14 ----------- .../20240322221521_user_token/migration.sql | 11 -------- .../migration.sql | 23 ----------------- .../migration.sql | 8 ------ .../20240328233304_init/migration.sql | 25 +++++++++++++++++++ .../schemas/migrations/migration_lock.toml | 2 +- src/database/schemas/schema.prisma | 4 ++- 8 files changed, 29 insertions(+), 70 deletions(-) delete mode 100644 src/database/schemas/migrations/20240227173707_auth_test/migration.sql delete mode 100644 src/database/schemas/migrations/20240314223153_remove_username/migration.sql delete mode 100644 src/database/schemas/migrations/20240322221521_user_token/migration.sql delete mode 100644 src/database/schemas/migrations/20240322222229_rename_user_token/migration.sql delete mode 100644 src/database/schemas/migrations/20240322222618_add_user_token_expires_at/migration.sql create mode 100644 src/database/schemas/migrations/20240328233304_init/migration.sql diff --git a/src/database/schemas/migrations/20240227173707_auth_test/migration.sql b/src/database/schemas/migrations/20240227173707_auth_test/migration.sql deleted file mode 100644 index 4754d8e..0000000 --- a/src/database/schemas/migrations/20240227173707_auth_test/migration.sql +++ /dev/null @@ -1,12 +0,0 @@ --- CreateTable -CREATE TABLE `User` ( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `email` VARCHAR(191) NOT NULL, - `username` VARCHAR(191) NOT NULL, - `password` VARCHAR(191) NOT NULL, - `is_admin` BOOLEAN NOT NULL, - - UNIQUE INDEX `User_email_key`(`email`), - UNIQUE INDEX `User_username_key`(`username`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/src/database/schemas/migrations/20240314223153_remove_username/migration.sql b/src/database/schemas/migrations/20240314223153_remove_username/migration.sql deleted file mode 100644 index 58a5948..0000000 --- a/src/database/schemas/migrations/20240314223153_remove_username/migration.sql +++ /dev/null @@ -1,14 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `username` on the `user` table. All the data in the column will be lost. - - You are about to alter the column `password` on the `user` table. The data in that column could be lost. The data in that column will be cast from `VarChar(191)` to `VarChar(128)`. - -*/ --- DropIndex -DROP INDEX `User_username_key` ON `user`; - --- AlterTable -ALTER TABLE `user` DROP COLUMN `username`, - MODIFY `email` VARCHAR(255) NOT NULL, - MODIFY `password` VARCHAR(128) NOT NULL; diff --git a/src/database/schemas/migrations/20240322221521_user_token/migration.sql b/src/database/schemas/migrations/20240322221521_user_token/migration.sql deleted file mode 100644 index d3121c9..0000000 --- a/src/database/schemas/migrations/20240322221521_user_token/migration.sql +++ /dev/null @@ -1,11 +0,0 @@ --- CreateTable -CREATE TABLE `UserToken` ( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `user_id` INTEGER NOT NULL, - `token` VARCHAR(255) NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- AddForeignKey -ALTER TABLE `UserToken` ADD CONSTRAINT `UserToken_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/src/database/schemas/migrations/20240322222229_rename_user_token/migration.sql b/src/database/schemas/migrations/20240322222229_rename_user_token/migration.sql deleted file mode 100644 index c739842..0000000 --- a/src/database/schemas/migrations/20240322222229_rename_user_token/migration.sql +++ /dev/null @@ -1,23 +0,0 @@ -/* - Warnings: - - - You are about to drop the `usertoken` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropForeignKey -ALTER TABLE `usertoken` DROP FOREIGN KEY `UserToken_user_id_fkey`; - --- DropTable -DROP TABLE `usertoken`; - --- CreateTable -CREATE TABLE `user_token` ( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `user_id` INTEGER NOT NULL, - `token` VARCHAR(255) NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- AddForeignKey -ALTER TABLE `user_token` ADD CONSTRAINT `user_token_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/src/database/schemas/migrations/20240322222618_add_user_token_expires_at/migration.sql b/src/database/schemas/migrations/20240322222618_add_user_token_expires_at/migration.sql deleted file mode 100644 index 64d6c2a..0000000 --- a/src/database/schemas/migrations/20240322222618_add_user_token_expires_at/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - Added the required column `expires_at` to the `user_token` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable -ALTER TABLE `user_token` ADD COLUMN `expires_at` DATETIME(3) NOT NULL; diff --git a/src/database/schemas/migrations/20240328233304_init/migration.sql b/src/database/schemas/migrations/20240328233304_init/migration.sql new file mode 100644 index 0000000..c9fcbbd --- /dev/null +++ b/src/database/schemas/migrations/20240328233304_init/migration.sql @@ -0,0 +1,25 @@ +-- CreateTable +CREATE TABLE "User" ( + "id" SERIAL NOT NULL, + "email" VARCHAR(255) NOT NULL, + "password" VARCHAR(128) NOT NULL, + "is_admin" BOOLEAN NOT NULL, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "user_token" ( + "id" SERIAL NOT NULL, + "user_id" INTEGER NOT NULL, + "token" VARCHAR(255) NOT NULL, + "expires_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "user_token_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- AddForeignKey +ALTER TABLE "user_token" ADD CONSTRAINT "user_token_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/src/database/schemas/migrations/migration_lock.toml b/src/database/schemas/migrations/migration_lock.toml index e5a788a..fbffa92 100644 --- a/src/database/schemas/migrations/migration_lock.toml +++ b/src/database/schemas/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "mysql" \ No newline at end of file +provider = "postgresql" \ No newline at end of file diff --git a/src/database/schemas/schema.prisma b/src/database/schemas/schema.prisma index 0b95725..7fa6c72 100644 --- a/src/database/schemas/schema.prisma +++ b/src/database/schemas/schema.prisma @@ -4,6 +4,8 @@ model User { password String @user_db.VarChar(128) is_admin Boolean tokens UserToken[] + + @@map("user") } model UserToken { @@ -17,7 +19,7 @@ model UserToken { } datasource user_db { - provider = "mysql" + provider = "postgresql" url = env("DATABASE_URL") } From 88e7bb74e6d2af64a54e60f3dde125f2b4506fd2 Mon Sep 17 00:00:00 2001 From: Andre Wlodkovski Date: Thu, 28 Mar 2024 20:41:34 -0300 Subject: [PATCH 2/4] Add rename user migration --- README.md | 2 +- .../20240328234107_rename_user/migration.sql | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/database/schemas/migrations/20240328234107_rename_user/migration.sql diff --git a/README.md b/README.md index fdfb16c..1885753 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ This step describes the prerequisites and install steps for the libraries needed ### Prerequisites - [Node.js 20+](https://nodejs.org/en) - [Node Package Manager (NPM)](https://www.npmjs.com) -- [MySQL](https://dev.mysql.com/downloads/installer/) +- [PostgreSQL](https://www.postgresql.org) ### Installing First of all, you must clone this repository: diff --git a/src/database/schemas/migrations/20240328234107_rename_user/migration.sql b/src/database/schemas/migrations/20240328234107_rename_user/migration.sql new file mode 100644 index 0000000..066e210 --- /dev/null +++ b/src/database/schemas/migrations/20240328234107_rename_user/migration.sql @@ -0,0 +1,27 @@ +/* + Warnings: + + - You are about to drop the `User` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "user_token" DROP CONSTRAINT "user_token_user_id_fkey"; + +-- DropTable +DROP TABLE "User"; + +-- CreateTable +CREATE TABLE "user" ( + "id" SERIAL NOT NULL, + "email" VARCHAR(255) NOT NULL, + "password" VARCHAR(128) NOT NULL, + "is_admin" BOOLEAN NOT NULL, + + CONSTRAINT "user_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "user_email_key" ON "user"("email"); + +-- AddForeignKey +ALTER TABLE "user_token" ADD CONSTRAINT "user_token_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE; From 23a358bbf5b478220c3f6c0cd94fd2842cba891f Mon Sep 17 00:00:00 2001 From: Andre Wlodkovski Date: Thu, 28 Mar 2024 20:43:25 -0300 Subject: [PATCH 3/4] Add AWS EKS deployment GitHub action --- .github/workflows/deploy-eks.yml | 76 ++++++++++++++++++++++++++++++++ .gitignore | 1 + kubernetes/aws/deployment.yml | 13 +++++- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/deploy-eks.yml diff --git a/.github/workflows/deploy-eks.yml b/.github/workflows/deploy-eks.yml new file mode 100644 index 0000000..b41c476 --- /dev/null +++ b/.github/workflows/deploy-eks.yml @@ -0,0 +1,76 @@ +name: Deploy to EKS + +on: + workflow_dispatch: + +env: + ECR_REPOSITORY: authentication-api + EKS_CLUSTER_NAME: default-cluster + AWS_REGION: us-east-1 + IMAGE_NAME: authentication-api + NODE_ENV: development + LOG_STRATEGY: CONSOLE + SECRETS_LIST: "" + + PASSWORD_SALT: "10" + JWT_SECRET: "" + JWT_EXPIRE_MINUTES: "60" + JWT_COOKIE_KEY: "JWT_COOKIE" + DATABASE_URL: "" + +jobs: + + build: + + name: Deployment + runs-on: ubuntu-latest + + steps: + + - name: Set short git commit SHA + id: commit + uses: prompt/actions-commit-hash@v2 + + - name: Check out code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{env.AWS_REGION}} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + registry-type: public + + - name: Login to Docker Hub (to pull images) + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build, tag, and push image to Amazon ECR + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ steps.commit.outputs.short }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \ + -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . + docker push -a $ECR_REGISTRY/$ECR_REPOSITORY + + - name: Update kube config + run: aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION + + - name: Deploy to EKS + env: + IMAGE_NAME: "${{ steps.login-ecr.outputs.registry }}/$IMAGE_NAME:${IMAGE_TAG}" + run: | + cat kubernetes/aws/deployment.yml | envsubst | kubectl apply -f - && \ + kubectl apply -f kubernetes/aws/service.yml \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6f32e7d..9897e88 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ web_modules/ .env.test.local .env.production.local .env.local +.secrets # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/kubernetes/aws/deployment.yml b/kubernetes/aws/deployment.yml index fa4c941..673a19b 100644 --- a/kubernetes/aws/deployment.yml +++ b/kubernetes/aws/deployment.yml @@ -26,7 +26,7 @@ spec: - name: SV_PORT value: "80" - name: NODE_ENV - value: "development" + value: $NODE_ENV - name: LOG_STRATEGY value: $LOG_STRATEGY - name: SECRETS_CONNECTOR @@ -34,6 +34,17 @@ spec: - name: SECRETS_LIST value: $SECRETS_LIST + - name: PASSWORD_SALT + value: $PASSWORD_SALT + - name: JWT_SECRET + value: $JWT_SECRET + - name: JWT_EXPIRE_MINUTES + value: $JWT_EXPIRE_MINUTES + - name: JWT_COOKIE_KEY + value: $JWT_COOKIE_KEY + - name: DATABASE_URL + value: $DATABASE_URL + resources: requests: memory: "64Mi" From 68c30da30004522e1fc9f35a6116692e19a37555 Mon Sep 17 00:00:00 2001 From: Andre Wlodkovski Date: Thu, 28 Mar 2024 20:43:59 -0300 Subject: [PATCH 4/4] Change app version to 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1badad7..e608966 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "authentication-api", - "version": "1.0.1", + "version": "1.1.0", "description": "Authentication REST API created with TypeScript", "main": "src/server.ts", "scripts": {