Compare commits
8
Commits
e9655740d5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af2358811a | ||
|
|
63badb0857 | ||
|
|
2597e5b352 | ||
|
|
6347e108fe | ||
|
|
c05bdaf679 | ||
|
|
950a38b2e2 | ||
|
|
67ac02403b | ||
|
|
1f871f59b9 |
-96
@@ -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
@@ -4,6 +4,8 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test-backend:
|
||||
@@ -82,29 +84,64 @@ jobs:
|
||||
- build-frontend
|
||||
|
||||
env:
|
||||
FRONTEND_URL: ${{ secrets.FRONTEND_URL }}
|
||||
BACKEND_URL: ${{ secrets.BACKEND_URL }}
|
||||
FRONTEND_URL: ${{ vars.FRONTEND_URL }}
|
||||
BACKEND_URL: ${{ vars.BACKEND_URL }}
|
||||
POSTGRES_URL: ${{ secrets.POSTGRES_URL }}
|
||||
REDIS_URL: ${{ secrets.REDIS_URL }}
|
||||
JWT_SECRET: ${{ secrets.JWT_SECRET }}
|
||||
CLIENT_ID: ${{ secrets.CLIENT_ID }}
|
||||
CLIENT_ID: ${{ vars.CLIENT_ID }}
|
||||
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
|
||||
CLIENT_NAME: ${{ secrets.CLIENT_NAME }}
|
||||
CLIENT_REDIRECT_URIS: ${{ secrets.CLIENT_REDIRECT_URIS }}
|
||||
DOMAIN: ${{ secrets.DOMAIN }}
|
||||
API_DOMAIN: ${{ secrets.API_DOMAIN }}
|
||||
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }}
|
||||
CLIENT_NAME: ${{ vars.CLIENT_NAME }}
|
||||
CLIENT_REDIRECT_URIS: ${{ vars.CLIENT_REDIRECT_URIS }}
|
||||
DOMAIN: ${{ vars.DOMAIN }}
|
||||
API_DOMAIN: ${{ vars.API_DOMAIN }}
|
||||
LETSENCRYPT_EMAIL: ${{ vars.LETSENCRYPT_EMAIL }}
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
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
|
||||
run: |
|
||||
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
|
||||
ssh -p 1650 -i ~/.ssh/deploy_key "${{ vars.DEPLOY_USER }}@${{ vars.DEPLOY_HOST }}" '
|
||||
set -e
|
||||
cd /opt/app
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@@ -66,23 +66,34 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
|
||||
}
|
||||
}
|
||||
|
||||
pipe := r.redis.Pipeline()
|
||||
incrCmd := pipe.Incr(ctx, counterKey)
|
||||
pipe.Expire(ctx, counterKey, cfg.Window)
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
// Fixed-window counter: only the request that brings the counter to 1
|
||||
// sets the TTL, so a burst never extends the window and the counter is
|
||||
// guaranteed to reset after cfg.Window even under sustained traffic.
|
||||
count64, err := r.redis.Incr(ctx, counterKey).Result()
|
||||
if err != nil {
|
||||
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())
|
||||
remaining := max(cfg.Max-count, 0)
|
||||
count := int(count64)
|
||||
|
||||
if count > cfg.Max {
|
||||
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
|
||||
}
|
||||
|
||||
remaining := max(cfg.Max-count, 0)
|
||||
return true, remaining, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,11 @@ export async function proxy(request: NextRequest) {
|
||||
|
||||
if (hasRefreshToken && session && pathname === "/login") {
|
||||
const accessToken = request.cookies.get("access_token")?.value;
|
||||
if (!accessToken) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
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",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -20,6 +23,11 @@ export async function proxy(request: NextRequest) {
|
||||
},
|
||||
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) {
|
||||
console.error("Failed to attach session:", error);
|
||||
return NextResponse.redirect(new URL("/callback/error", request.url));
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user