-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-ory.sh
More file actions
289 lines (259 loc) · 6.77 KB
/
setup-ory.sh
File metadata and controls
289 lines (259 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
echo "Setting up Ory Hydra and Kratos..."
# Create necessary directories
mkdir -p scripts config/kratos
# Create PostgreSQL init script
cat > scripts/init-multiple-postgres-dbs.sh << 'END'
#!/bin/bash
set -e
set -u
function create_database() {
local database=$1
echo "Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_database $db
done
echo "Multiple databases created"
fi
END
chmod +x scripts/init-multiple-postgres-dbs.sh
# Create Kratos identity schema
cat > config/kratos/identity.schema.json << 'END'
{
"$id": "https://schemas.ory.sh/presets/kratos/identity.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"title": "E-Mail",
"minLength": 3,
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
}
},
"verification": {
"via": "email"
},
"recovery": {
"via": "email"
}
}
},
"name": {
"type": "object",
"properties": {
"first": {
"type": "string",
"title": "First Name"
},
"last": {
"type": "string",
"title": "Last Name"
}
}
}
},
"required": [
"email"
],
"additionalProperties": false
}
}
}
END
# Create Kratos configuration
cat > config/kratos/kratos.yml << 'END'
version: v1.0.0
dsn: postgres://ory:ory_password@postgres:5432/kratos?sslmode=disable
serve:
public:
base_url: http://localhost:4433/
cors:
enabled: true
admin:
base_url: http://localhost:4434/
selfservice:
default_browser_return_url: http://localhost:3000/
allowed_return_urls:
- http://localhost:3000/
methods:
password:
enabled: true
flows:
login:
ui_url: http://localhost:3000/login
lifespan: 10m
registration:
ui_url: http://localhost:3000/registration
lifespan: 10m
after:
password:
hooks:
- hook: session
log:
level: debug
format: text
secrets:
cookie:
- PLEASE-CHANGE-ME-I-AM-VERY-INSECURE
cipher:
- 32-LONG-SECRET-NOT-SECURE-AT-ALL
identity:
default_schema_id: default
schemas:
- id: default
url: file:///etc/config/kratos/identity.schema.json
END
# Create Docker Compose file
cat > docker-compose.yml << 'END'
version: '3.7'
services:
postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_USER=ory
- POSTGRES_PASSWORD=ory_password
- POSTGRES_DB=ory
- POSTGRES_MULTIPLE_DATABASES=hydra,kratos
volumes:
- postgres-data:/var/lib/postgresql/data
- ./scripts/init-multiple-postgres-dbs.sh:/docker-entrypoint-initdb.d/init-multiple-postgres-dbs.sh
networks:
- ory_network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ory"]
interval: 10s
timeout: 5s
retries: 5
hydra-migrate:
image: oryd/hydra:v2.2.0
depends_on:
postgres:
condition: service_healthy
environment:
- DSN=postgres://ory:ory_password@postgres:5432/hydra?sslmode=disable
command: migrate sql -e --yes
networks:
- ory_network
restart: on-failure
hydra:
image: oryd/hydra:v2.2.0
depends_on:
hydra-migrate:
condition: service_completed_successfully
ports:
- "4444:4444" # Public API
- "4445:4445" # Admin API
environment:
- DSN=postgres://ory:ory_password@postgres:5432/hydra?sslmode=disable
- SERVE_PUBLIC_PORT=4444
- SERVE_ADMIN_PORT=4445
- URLS_SELF_ISSUER=http://localhost:4444/
- URLS_CONSENT=http://localhost:3000/consent
- URLS_LOGIN=http://localhost:3000/login
- SECRETS_SYSTEM=youReallyNeedToChangeThis
- OIDC_SUBJECT_IDENTIFIERS_SUPPORTED_TYPES=public,pairwise
- OIDC_SUBJECT_IDENTIFIERS_PAIRWISE_SALT=youReallyNeedToChangeThis
- LOG_LEVEL=debug
networks:
- ory_network
restart: unless-stopped
command: serve all --dev
kratos-migrate:
image: oryd/kratos:v1.0.0
depends_on:
postgres:
condition: service_healthy
environment:
- DSN=postgres://ory:ory_password@postgres:5432/kratos?sslmode=disable
volumes:
- ./config/kratos:/etc/config/kratos
command: migrate sql -e --yes
networks:
- ory_network
restart: on-failure
kratos:
image: oryd/kratos:v1.0.0
depends_on:
kratos-migrate:
condition: service_completed_successfully
ports:
- "4433:4433" # Public API
- "4434:4434" # Admin API
environment:
- DSN=postgres://ory:ory_password@postgres:5432/kratos?sslmode=disable
- LOG_LEVEL=debug
volumes:
- ./config/kratos:/etc/config/kratos
networks:
- ory_network
restart: unless-stopped
command: serve -c /etc/config/kratos/kratos.yml --dev
networks:
ory_network:
external: true
volumes:
postgres-data:
END
# Start the services
echo "Starting services..."
docker compose up -d
echo "Waiting for services to be ready..."
sleep 15
echo "Creating a test user..."
docker exec -it $(docker ps -qf "name=kratos") kratos identities import -r < <(cat << 'EOF'
{
"identity": {
"schema_id": "default",
"traits": {
"email": "test@example.com",
"name": {
"first": "Test",
"last": "User"
}
}
},
"password": "password123"
}
EOF
)
echo "Creating OAuth2 client..."
docker exec -it $(docker ps -qf "name=hydra") hydra clients create \
--endpoint http://localhost:4445 \
--id test-client \
--secret test-secret \
--grant-type authorization_code,refresh_token,client_credentials \
--response-type code,id_token \
--scope openid,offline \
--redirect-uri http://localhost:3000/callback
echo "Setup complete!"
echo "User credentials:"
echo " Email: test@example.com"
echo " Password: password123"
echo ""
echo "Client credentials:"
echo " Client ID: test-client"
echo " Client Secret: test-secret"
echo ""
echo "APIs are available at:"
echo " Kratos Public API: http://localhost:4433"
echo " Kratos Admin API: http://localhost:4434"
echo " Hydra Public API: http://localhost:4444"
echo " Hydra Admin API: http://localhost:4445"