-
Notifications
You must be signed in to change notification settings - Fork 83
/
Makefile
269 lines (224 loc) · 9.89 KB
/
Makefile
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
include .env-issuer
BIN := $(shell pwd)/bin
VERSION ?= $(shell git rev-parse --short HEAD)
GO?=$(shell which go)
export GOBIN := $(BIN)
export PATH := $(BIN):$(PATH)
BUILD_CMD := $(GO) install -ldflags "-X main.build=${VERSION}"
LOCAL_DEV_PATH = $(shell pwd)/infrastructure/local
DOCKER_COMPOSE_FILE := $(LOCAL_DEV_PATH)/docker-compose.yml
DOCKER_COMPOSE_FILE_INFRA := $(LOCAL_DEV_PATH)/docker-compose-infra.yml
DOCKER_COMPOSE_FULL_FILE := $(LOCAL_DEV_PATH)/docker-compose-full.yml
DOCKER_COMPOSE_CMD := docker compose -p issuer -f $(DOCKER_COMPOSE_FILE)
DOCKER_COMPOSE_FULL_CMD := docker compose -p issuer -f $(DOCKER_COMPOSE_FULL_FILE)
DOCKER_COMPOSE_INFRA_CMD := docker compose -p issuer -f $(DOCKER_COMPOSE_FILE_INFRA)
ENVIRONMENT := ${ISSUER_ENVIRONMENT}
ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH := ${ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH}
ISSUER_KMS_ETH_PROVIDER := ${ISSUER_KMS_ETH_PROVIDER}
ISSUER_KMS_BJJ_PROVIDER := ${ISSUER_KMS_BJJ_PROVIDER}
ISSUER_RESOLVER_FILE := ${ISSUER_RESOLVER_FILE}
REQUIRED_FILE := ${ISSUER_RESOLVER_PATH}
# Local environment overrides via godotenv
DOTENV_CMD = $(BIN)/godotenv
ENV = $(DOTENV_CMD) -f .env-issuer
.PHONY: run-all-registry
run-all-registry:
@make down
ifeq ($(ISSUER_KMS_ETH_PROVIDER)$(ISSUER_KMS_BJJ_PROVIDER), localstoragelocalstorage)
$(DOCKER_COMPOSE_FULL_CMD) up -d redis postgres api pending_publisher notifications ui
else
$(DOCKER_COMPOSE_FULL_CMD) up -d redis postgres vault api pending_publisher notifications ui
endif
.PHONY: build-local
build-local:
$(BUILD_CMD) ./cmd/...
.PHONY: build/docker
build/docker: ## Build the docker image.
DOCKER_BUILDKIT=1 \
docker build \
-f ./Dockerfile \
-t issuer/api:$(VERSION) \
--build-arg VERSION=$(VERSION) \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
.
.PHONY: tests
tests:
$(GO) test -v ./... --count=1
.PHONY: test-race
test-race:
$(GO) test -v --race ./...
$(BIN)/oapi-codegen: tools.go go.mod go.sum ## install code generator for API files.
$(GO) install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
.PHONY: api
api: $(BIN)/oapi-codegen
$(BIN)/oapi-codegen -config ./api/config-oapi-codegen.yaml ./api/api.yaml > ./internal/api/api.gen.go
# Starts the infrastructure services
.PHONY: up
up:
ifeq ($(ISSUER_KMS_ETH_PROVIDER)$(ISSUER_KMS_BJJ_PROVIDER), localstoragelocalstorage)
$(DOCKER_COMPOSE_INFRA_CMD) up -d redis postgres
else
$(DOCKER_COMPOSE_INFRA_CMD) up -d redis postgres vault
endif
# If you want to use localstorage as a KMS provider, you need to run this command
.PHONY: up/localstorage
up/localstorage:
$(DOCKER_COMPOSE_INFRA_CMD) up -d redis postgres
# Build the docker image for the issuer-api
.PHONY: build-api
build-api:
docker build -t issuernode-api:local -f ./Dockerfile .
# Build the docker image for the issuer-ui
.PHONY: build-ui
build-ui:
docker build -t issuernode-ui:local -f ./ui/Dockerfile ./ui
.PHONY: validate_issuer_resolver_file
validate_issuer_resolver_file:
@if [ ! -f "$(REQUIRED_FILE)" ]; then \
if [ -z "$(ISSUER_RESOLVER_FILE)" ]; then \
echo "ISSUER_RESOLVER_FILE env var is empty, and the file $(REQUIRED_FILE) doesn't exists."; \
exit 1; \
else \
echo "ISSUER_RESOLVER_FILE is set, using it..."; \
fi \
else \
echo "$(REQUIRED_FILE) environment is present, using it "; \
fi
.PHONY: validate_localstorage_file
validate_localstorage_file:
@if [ "$(ISSUER_KMS_ETH_PROVIDER)" = "localstorage" ]; then \
if [ ! -f "$(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json" ]; then \
mkdir -p $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH); \
touch $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
echo "[]" > $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
fi \
fi
@if [ "$(ISSUER_KMS_BJJ_PROVIDER)" = "localstorage" ]; then \
if [ ! -f "$(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json" ]; then \
mkdir -p $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH); \
touch $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
echo "[]" > $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
fi \
fi
# Run the api, pending_publisher and notifications services
.PHONY: run-api
run-api: validate_issuer_resolver_file validate_localstorage_file up
COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKER_COMPOSE_CMD) up -d api pending_publisher notifications
# Run the ui.
# First build the ui image and the api image
.PHONY: run-ui
run-ui: build-ui add-host-url-swagger
COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKER_COMPOSE_CMD) up -d ui
# Run all services
.PHONE: run-all
run-all: build-api build-ui validate_localstorage_file up add-host-url-swagger
COMPOSE_DOCKER_CLI_BUILD=1 $(DOCKER_COMPOSE_CMD) up -d ui api pending_publisher notifications
.PHONY: down
down:
$(DOCKER_COMPOSE_INFRA_CMD) down --remove-orphans
$(DOCKER_COMPOSE_CMD) down --remove-orphans
.PHONY: stop
stop:
$(DOCKER_COMPOSE_CMD) stop
.PHONY: stop-all
stop-all:
$(DOCKER_COMPOSE_INFRA_CMD) stop
$(DOCKER_COMPOSE_CMD) stop
.PHONY: up-test
up-test:
$(DOCKER_COMPOSE_INFRA_CMD) up -d test_postgres vault test_local_files_apache
$(BIN)/platformid-migrate:
$(BUILD_CMD) ./cmd/migrate
$(BIN)/install-goose: go.mod go.sum
$(GO) install github.com/pressly/goose/v3
$(BIN)/golangci-lint: go.mod go.sum
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint
$(BIN)/godotenv: tools.go go.mod go.sum
$(GO) install github.com/joho/godotenv/cmd/godotenv
.PHONY: db/migrate
db/migrate: $(BIN)/install-goose $(BIN)/godotenv $(BIN)/platformid-migrate ## Install goose and apply migrations.
$(ENV) sh -c '$(BIN)/migrate'
.PHONY: lint
lint: $(BIN)/golangci-lint
$(BIN)/golangci-lint run
.PHONY: lint-fix
lint-fix: $(BIN)/golangci-lint
$(BIN)/golangci-lint run --fix
## Usage:
## AWS: make private_key=XXX aws_access_key=YYY aws_secret_key=ZZZ aws_region=your-region import-private-key-to-kms
## localstorage and vault: make private_key=XXX import-private-key-to-kms
.PHONY: import-private-key-to-kms
import-private-key-to-kms:
ifeq ($(ISSUER_KMS_ETH_PROVIDER), aws)
@echo ">>> importing private key to AWS KMS"
docker build --build-arg ISSUER_KMS_ETH_PROVIDER_AWS_ACCESS_KEY=$(aws_access_key) \
--build-arg ISSUER_KMS_ETH_PROVIDER_AWS_SECRET_KEY=$(aws_secret_key) \
--build-arg ISSUER_KMS_ETH_PROVIDER_AWS_REGION=$(aws_region) -t privadoid-kms-importer -f ./Dockerfile-kms-importer .
$(eval result = $(shell docker run -it -v ./.env-issuer:/.env-issuer \
--network issuer-network \
privadoid-kms-importer ./kms_priv_key_importer --privateKey=$(private_key)))
@echo "result: $(result)"
$(eval keyID = $(shell echo $(result) | grep "key created keyId=" | sed 's/.*keyId=//'))
@if [ -n "$(keyID)" ]; then \
docker run -it --rm -v ./.env-issuer:/.env-issuer --network issuer-network \
privadoid-kms-importer sh ./aws_kms_material_key_importer.sh $(private_key) $(keyID) privadoid; \
else \
echo "something went wrong because keyID is empty"; \
fi
else ifeq ($(ISSUER_KMS_ETH_PROVIDER), localstorage)
echo ">>> importing private key to LOCALSTORAGE"
@docker build -t privadoid-kms-importer -f ./Dockerfile-kms-importer .
@if [ ! -f "$(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json" ]; then \
mkdir -p $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH); \
touch $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
echo "[]" > $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json; \
fi
docker run --rm -it -v ./.env-issuer:/.env-issuer -v $(ISSUER_KMS_PROVIDER_LOCAL_STORAGE_FILE_PATH)/kms_localstorage_keys.json:/localstoragekeys/kms_localstorage_keys.json \
privadoid-kms-importer ./kms_priv_key_importer --privateKey=$(private_key)
else ifeq ($(ISSUER_KMS_ETH_PROVIDER), vault)
@echo ">>> importing private key to VAULT"
@docker build -t privadoid-kms-importer -f ./Dockerfile-kms-importer .
$(eval NETWORK=$(shell docker inspect issuer-vault-1 --format '{{ .HostConfig.NetworkMode }}'))
@echo $(NETWORK)
docker run --rm -it -v ./.env-issuer:/.env-issuer --network $(NETWORK) \
privadoid-kms-importer ./kms_priv_key_importer --privateKey=$(private_key)
else
@echo "ISSUER_KMS_ETH_PROVIDER is not set"
endif
.PHONY: print-vault-token
print-vault-token:
$(eval TOKEN = $(shell docker logs issuer-vault-1 2>&1 | grep " .hvs" | awk '{print $$2}' | tail -1 ))
echo $(TOKEN)
.PHONY: add-vault-token
add-vault-token:
$(eval TOKEN = $(shell docker logs issuer-vault-1 2>&1 | grep " .hvs" | awk '{print $$2}' | tail -1 ))
sed '/ISSUER_KEY_STORE_TOKEN/d' .env-issuer > .env-issuer.tmp
@echo ISSUER_KEY_STORE_TOKEN=$(TOKEN) >> .env-issuer.tmp
mv .env-issuer.tmp .env-issuer
.PHONY: add-host-url-swagger
add-host-url-swagger:
@if [ $(ENVIRONMENT) != "" ] && [ $(ENVIRONMENT) != "local" ]; then \
sed -i -e "s#server-url = [^ ]*#server-url = \""${ISSUER_API_UI_SERVER_URL}"\"#g" api_ui/spec.html; \
fi
# usage: make vault_token=xxx vault-export-keys
.PHONY: vault-export-keys
vault-export-keys:
docker build -t issuer-vault-export-keys .
docker run --rm -it --network=issuer-network -v $(shell pwd):/keys issuer-vault-export-keys ./vault-migrator -operation=export -output-file=keys.json -vault-token=$(vault_token) -vault-addr=http://vault:8200
# usage: make vault_token=xxx vault-import-keys
.PHONY: vault-import-keys
vault-import-keys:
docker build -t issuer-vault-import-keys .
docker run --rm -it --network=issuer-network -v $(shell pwd)/keys.json:/keys.json issuer-vault-import-keys ./vault-migrator -operation=import -input-file=keys.json -vault-token=$(vault_token) -vault-addr=http://vault:8200
# usage: make new_password=xxx change-vault-password
.PHONY: change-vault-password
change-vault-password:
docker exec issuer-vault-1 \
vault write auth/userpass/users/issuernode password=$(new_password)
.PHONY: print-commands
print-commands:
@grep '^\s*\.[a-zA-Z_][a-zA-Z0-9_]*' Makefile
.PHONY: clean-volumes
clean-volumes:
$(DOCKER_COMPOSE_INFRA_CMD) down -v
$(DOCKER_COMPOSE_FULL_CMD) down -v