Compare commits

..
8 Commits
Author SHA1 Message Date
Ivan Rezchikov af2358811a fix: ratelimiter fixed
Build and Deploy / lint-frontend (push) Canceled after 0s
Build and Deploy / build-backend (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 0s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / test-backend (push) Canceled after 10s
2026-09-18 16:02:48 +03:00
Ivan Rezchikov 63badb0857 fix: oauth flow
Build and Deploy / test-backend (push) Successful in 1m5s
Build and Deploy / lint-frontend (push) Successful in 1m28s
Build and Deploy / build-backend (push) Successful in 3s
Build and Deploy / build-frontend (push) Successful in 41s
Build and Deploy / deploy (push) Successful in 5s
2026-09-17 23:50:36 +03:00
Ivan Rezchikov 2597e5b352 fix: fixed redirecting
Build and Deploy / test-backend (push) Successful in 1m15s
Build and Deploy / lint-frontend (push) Successful in 1m13s
Build and Deploy / build-backend (push) Successful in 50s
Build and Deploy / build-frontend (push) Successful in 40s
Build and Deploy / deploy (push) Successful in 6s
2026-09-17 13:12:54 +03:00
Ivan Rezchikov 6347e108fe fix: proxy fixed
Build and Deploy / test-backend (push) Successful in 1m31s
Build and Deploy / lint-frontend (push) Successful in 1m54s
Build and Deploy / build-backend (push) Successful in 7s
Build and Deploy / build-frontend (push) Successful in 1m0s
Build and Deploy / deploy (push) Successful in 8s
2026-09-16 17:40:25 +03:00
Ivan Rezchikov c05bdaf679 ci/cd: test
Build and Deploy / test-backend (push) Successful in 56s
Build and Deploy / lint-frontend (push) Successful in 1m26s
Build and Deploy / build-backend (push) Successful in 5s
Build and Deploy / build-frontend (push) Successful in 9s
Build and Deploy / deploy (push) Successful in 4s
2026-09-15 19:36:41 +03:00
Ivan Rezchikov 950a38b2e2 ci/cd: ssh deployment
Build and Deploy / test-backend (push) Successful in 1m0s
Build and Deploy / lint-frontend (push) Successful in 2m3s
Build and Deploy / build-backend (push) Successful in 4s
Build and Deploy / build-frontend (push) Successful in 3s
Build and Deploy / deploy (push) Successful in 4s
2026-09-15 19:23:20 +03:00
Ivan Rezchikov 67ac02403b ci/cd: variables and secrets
Build and Deploy / test-backend (push) Successful in 55s
Build and Deploy / lint-frontend (push) Successful in 1m23s
Build and Deploy / build-backend (push) Successful in 5s
Build and Deploy / build-frontend (push) Successful in 5s
Build and Deploy / deploy (push) Successful in 4s
2026-09-15 18:48:20 +03:00
Ivan Rezchikov 1f871f59b9 ci/cd: secrets rotation
Build and Deploy / test-backend (push) Successful in 1m1s
Build and Deploy / lint-frontend (push) Successful in 1m28s
Build and Deploy / build-backend (push) Successful in 4s
Build and Deploy / build-frontend (push) Successful in 4s
Build and Deploy / deploy (push) Successful in 4s
2026-09-15 18:23:39 +03:00
6 changed files with 81 additions and 121 deletions
-96
View File
@@ -1,96 +0,0 @@
kind: pipeline
type: docker
name: default
steps:
- name: test backend
image: golang:1.25-alpine3.21
commands:
- cd kotosoft-id-backend
- go mod tidy
- go test ./...
- name: lint frontend
image: node:20-alpine
commands:
- cd kotosoft-id-frontend
- npm install
- npm run lint
- name: build and push backend
image: docker:24
volumes:
- name: dockersock
path: /var/run/docker.sock
- name: appdir
path: /opt/app
commands:
- docker build
-t 172.17.0.1:5000/kotosoft_id_backend:latest
-f kotosoft-id-backend/Dockerfile
kotosoft-id-backend
- docker push 172.17.0.1:5000/kotosoft_id_backend:latest
- name: build and push frontend
image: docker:24
volumes:
- name: dockersock
path: /var/run/docker.sock
commands:
- docker build
-t 172.17.0.1:5000/kotosoft_id_frontend:latest
-f kotosoft-id-frontend/Dockerfile
kotosoft-id-frontend
- docker push 172.17.0.1:5000/kotosoft_id_frontend:latest
- name: deploy
image: docker:24
volumes:
- name: dockersock
path: /var/run/docker.sock
- name: appdir
path: /opt/app
environment:
FRONTEND_URL:
from_secret: FRONTEND_URL
BACKEND_URL:
from_secret: BACKEND_URL
POSTGRES_URL:
from_secret: POSTGRES_URL
REDIS_URL:
from_secret: REDIS_URL
JWT_SECRET:
from_secret: JWT_SECRET
CLIENT_ID:
from_secret: CLIENT_ID
CLIENT_SECRET:
from_secret: CLIENT_SECRET
CLIENT_NAME:
from_secret: CLIENT_NAME
CLIENT_REDIRECT_URIS:
from_secret: CLIENT_REDIRECT_URIS
DOMAIN:
from_secret: DOMAIN
API_DOMAIN:
from_secret: API_DOMAIN
LETSENCRYPT_EMAIL:
from_secret: LETSENCRYPT_EMAIL
commands:
- apk add --no-cache docker-cli-compose
- mkdir -p /opt/app
- cp docker-compose.yml /opt/app/docker-compose.yml
- cd /opt/app
- docker compose -p kotosoft pull
- docker compose -p kotosoft up -d --remove-orphans
volumes:
- name: dockersock
host:
path: /var/run/docker.sock
- name: appdir
host:
path: /opt/drone/kotosoft-id
trigger:
branch:
- main
+52 -15
View File
@@ -4,6 +4,8 @@ on:
push: push:
branches: branches:
- main - main
workflow_dispatch:
jobs: jobs:
test-backend: test-backend:
@@ -82,29 +84,64 @@ jobs:
- build-frontend - build-frontend
env: env:
FRONTEND_URL: ${{ secrets.FRONTEND_URL }} FRONTEND_URL: ${{ vars.FRONTEND_URL }}
BACKEND_URL: ${{ secrets.BACKEND_URL }} BACKEND_URL: ${{ vars.BACKEND_URL }}
POSTGRES_URL: ${{ secrets.POSTGRES_URL }} POSTGRES_URL: ${{ secrets.POSTGRES_URL }}
REDIS_URL: ${{ secrets.REDIS_URL }} REDIS_URL: ${{ secrets.REDIS_URL }}
JWT_SECRET: ${{ secrets.JWT_SECRET }} JWT_SECRET: ${{ secrets.JWT_SECRET }}
CLIENT_ID: ${{ secrets.CLIENT_ID }} CLIENT_ID: ${{ vars.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }} CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
CLIENT_NAME: ${{ secrets.CLIENT_NAME }} CLIENT_NAME: ${{ vars.CLIENT_NAME }}
CLIENT_REDIRECT_URIS: ${{ secrets.CLIENT_REDIRECT_URIS }} CLIENT_REDIRECT_URIS: ${{ vars.CLIENT_REDIRECT_URIS }}
DOMAIN: ${{ secrets.DOMAIN }} DOMAIN: ${{ vars.DOMAIN }}
API_DOMAIN: ${{ secrets.API_DOMAIN }} API_DOMAIN: ${{ vars.API_DOMAIN }}
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }} LETSENCRYPT_EMAIL: ${{ vars.LETSENCRYPT_EMAIL }}
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Prepare SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -p 1650 -H "${{ vars.DEPLOY_HOST }}" >> ~/.ssh/known_hosts
- name: Verify SSH key
run: |
ls -la ~/.ssh/deploy_key
head -1 ~/.ssh/deploy_key
tail -1 ~/.ssh/deploy_key
- name: Build .env for remote
run: |
cat > .env <<EOF
FRONTEND_URL=${FRONTEND_URL}
BACKEND_URL=${BACKEND_URL}
POSTGRES_URL=${POSTGRES_URL}
REDIS_URL=${REDIS_URL}
JWT_SECRET=${JWT_SECRET}
CLIENT_ID=${CLIENT_ID}
CLIENT_SECRET=${CLIENT_SECRET}
CLIENT_NAME=${CLIENT_NAME}
CLIENT_REDIRECT_URIS=${CLIENT_REDIRECT_URIS}
DOMAIN=${DOMAIN}
API_DOMAIN=${API_DOMAIN}
LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
EOF
- name: Copy compose file and env
run: |
scp -P 1650 -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
docker-compose.yml .env \
"${{ vars.DEPLOY_USER }}@${{ vars.DEPLOY_HOST }}:/opt/app/"
- name: Deploy - name: Deploy
run: | run: |
mkdir -p /opt/app ssh -p 1650 -i ~/.ssh/deploy_key "${{ vars.DEPLOY_USER }}@${{ vars.DEPLOY_HOST }}" '
cp docker-compose.yml /opt/app/docker-compose.yml set -e
cd /opt/app
cd /opt/app docker compose -p kotosoft pull
docker compose -p kotosoft up -d --remove-orphans
docker compose -p kotosoft pull '
docker compose -p kotosoft up -d --remove-orphans
@@ -32,7 +32,7 @@ func (h *OAuthHandler) Authorize(c *gin.Context) {
return return
} }
c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token) c.Redirect(http.StatusFound, h.frontendURL+"/login?session="+token)
} }
func (h *OAuthHandler) GetSession(c *gin.Context) { func (h *OAuthHandler) GetSession(c *gin.Context) {
@@ -66,23 +66,34 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
} }
} }
pipe := r.redis.Pipeline() // Fixed-window counter: only the request that brings the counter to 1
incrCmd := pipe.Incr(ctx, counterKey) // sets the TTL, so a burst never extends the window and the counter is
pipe.Expire(ctx, counterKey, cfg.Window) // guaranteed to reset after cfg.Window even under sustained traffic.
if _, err := pipe.Exec(ctx); err != nil { count64, err := r.redis.Incr(ctx, counterKey).Result()
if err != nil {
return false, 0, fmt.Errorf("rate limiter: %w", err) return false, 0, fmt.Errorf("rate limiter: %w", err)
} }
if count64 == 1 {
if err := r.redis.Expire(ctx, counterKey, cfg.Window).Err(); err != nil {
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
}
count := int(incrCmd.Val()) count := int(count64)
remaining := max(cfg.Max-count, 0)
if count > cfg.Max { if count > cfg.Max {
if cfg.BlockFor > 0 { if cfg.BlockFor > 0 {
_ = r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err() // Reset the counter when blocking so that once the block expires
// the client is not instantly re-blocked by the stale count.
_ = r.redis.Del(ctx, counterKey).Err()
if err := r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err(); err != nil {
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
} }
return false, 0, nil return false, 0, nil
} }
remaining := max(cfg.Max-count, 0)
return true, remaining, nil return true, remaining, nil
} }
+9 -1
View File
@@ -11,8 +11,11 @@ export async function proxy(request: NextRequest) {
if (hasRefreshToken && session && pathname === "/login") { if (hasRefreshToken && session && pathname === "/login") {
const accessToken = request.cookies.get("access_token")?.value; const accessToken = request.cookies.get("access_token")?.value;
if (!accessToken) {
return NextResponse.next();
}
try { try {
await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, { const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -20,6 +23,11 @@ export async function proxy(request: NextRequest) {
}, },
body: JSON.stringify({ session }), body: JSON.stringify({ session }),
}); });
if (!res.ok) throw new Error("attach failed");
const data = await res.json().catch(() => null);
if (data?.redirect_url) {
return NextResponse.redirect(new URL(data.redirect_url));
}
} catch (error) { } catch (error) {
console.error("Failed to attach session:", error); console.error("Failed to attach session:", error);
return NextResponse.redirect(new URL("/callback/error", request.url)); return NextResponse.redirect(new URL("/callback/error", request.url));
File diff suppressed because one or more lines are too long