forked from RedHatInsights/authz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
211 lines (184 loc) · 7.51 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
GO := go
GOFMT := gofmt
PROTOC_INSTALLED := $(shell which protoc 2>/dev/null)
BUF_INSTALLED := $(shell which buf 2>/dev/null)
GEN_GW_INSTALLED := $(shell which protoc-gen-grpc-gateway 2>/dev/null)
GEN_OPENAPI_INSTALLED := $(shell which protoc-gen-openapiv2 2>/dev/null)
GEN_GO_GRPC_INSTALLED := $(shell which protoc-gen-go-grpc 2>/dev/null)
GEN_GO_INSTALLED := $(shell which protoc-gen-go 2>/dev/null)
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell $(GO) env GOBIN))
GOBIN=$(shell $(GO) env GOPATH)/bin
else
GOBIN=$(shell $(GO) env GOBIN)
endif
DOCKER ?= docker
DOCKER_CONFIG="${PWD}/.docker"
SHELL = bash
# builds the binary inside the bin folder
.PHONY: binary
binary:
@echo "Building the service..."
$(GO) build -tags release -o bin/authz cmd/main.go
# builds the binary inside the bin folder
.PHONY: binary-delete
binary-delete:
@echo "removing binary"
@rm -rf bin/
@echo "binary successfully removed"
# starts a kind cluster
.PHONY: kind-create
kind-create:
@kind create cluster --name=authz --config=k8s/kind.yml
@kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
# make kind-deploy (below) fails (admission controller fails on ingress) unless nginx controller pod is ready.
@echo "Waiting for nginx to become available..."
@kubectl wait --for condition=Available=True deployment.apps/ingress-nginx-controller -n ingress-nginx --timeout 20s
@kubectl wait --for=condition=complete job/ingress-nginx-admission-create -n ingress-nginx --timeout 60s
@kubectl wait --for=condition=complete job/ingress-nginx-admission-patch -n ingress-nginx --timeout 60s
@kubectl wait --for condition=Ready=True pod -l app.kubernetes.io/component=controller -n ingress-nginx --timeout 120s
# deploys the authz server to the kind cluster
.PHONY: kind-deploy
kind-deploy:
@kubectl apply -f k8s/authz.yml
@echo "Wait for pods with: kubectl get pods,svc"
@echo "See k8s/README.md for more info."
# adds the spiceDB schema to the kind clusters configmap
.PHONY: kind-create-schema-configmap
kind-create-schema-configmap:
@kubectl create configmap spicedb-schema --from-file=schema/
# deploys spiceDB
.PHONY: kind-spicedb-deploy
kind-spicedb-deploy:
@kubectl create secret generic spicedb --from-file=SPICEDB_GRPC_PRESHARED_KEY=.secrets/spice-db-local
@kubectl apply -f k8s/spicedb.yaml
# deletes the kind cluster
.PHONY: kind-delete
kind-delete:
@kind delete cluster --name=authz
# creates tls certificate and key in the tls dir
.PHONY: tls-cert
tls-cert:
@echo "creating directory tls/"
@mkdir -p tls
@echo "Generating self-signed TLS certs. needs openssl 1.1.1 or newer..."
@openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
-keyout tls/tls.key -out tls/tls.crt -subj "/CN=localhost" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1"
@echo "Success! find your cert files in the tls/ folder"
# delete the tls directory and certs
.PHONY: tls-delete
tls-delete:
@echo "removing generated tls certs"
@rm -rf tls/
@echo "TLS certs successfully removed"
# validate the openapi schema
.PHONY: apigen-validate
apigen-validate:
$(DOCKER) run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli validate -i /local/api/v1alpha/openapi-authz-v1alpha.yaml
# Run Swagger UI and host the api docs
.PHONY: apidocs-start
apidocs-ui:
$(DOCKER) run --rm --name swagger_ui_docs -d -p 8082:8080 -e URLS="[ \
{ url: \"/openapi/v1alpha/openapi-authz-v1alpha.yaml\", name: \"Authz API\"}]"\
-v $(PWD)/api/:/usr/share/nginx/html/openapi:Z swaggerapi/swagger-ui
@echo "Please open http://localhost:8082/"
# Remove Swagger container
.PHONY: apidocs-stop
apidocs-stop:
$(DOCKER) container stop swagger_ui_docs
$(DOCKER) container rm swagger_ui_docs
#convert grpc gateway openapiv2 spec to openapi v3
.PHONY: apigen-v3
apigen-v3:
@echo "generating v3 openapi.yaml from grpc-gateway v2 yaml..."
$(DOCKER) run --rm --name swagger_codegen \
-v $(PWD)/api/gen/v1alpha/:/opt/mnt:Z -w /opt/mnt swaggerapi/swagger-codegen-cli-v3:3.0.41 generate -i ./core.swagger.yaml -l openapi-yaml -o .
@echo "generating v3 openapi.json from grpc-gateway v2 json..."
$(DOCKER) run --rm --name swagger_codegen \
-v $(PWD)/api/gen/v1alpha/:/opt/mnt:Z -w /opt/mnt swaggerapi/swagger-codegen-cli-v3:3.0.41 generate -i ./core.swagger.json -l openapi -o .
@echo "Remove unnecessary generated artifacts"
@make apigen-v2-delete
@echo "move and rename files to v1alpha directory"
@cd api/gen/v1alpha && mv openapi.yaml ../../v1alpha/openapi-authz-v1alpha.yaml
@cd api/gen/v1alpha && mv openapi.json ../../v1alpha/openapi-authz-v1alpha.json
# remove generated openAPI artifacts
.PHONY: apigen-v2-delete
apigen-v2-delete:
@echo "removing openapi v2 artifacts"
@cd api/gen/v1alpha && rm -rf .swagger-codegen/ && rm -f README.md && rm -f .swagger-codegen-ignore
@echo "opanpi v2 artifacts successfully removed"
# Generate grpc gateway code from proto via buf
.PHONY: buf-gen
buf-gen:
#check if protoc is installed
ifndef PROTOC_INSTALLED
$(error "protoc is not installed, please install protoc - see https://grpc.io/docs/protoc-installation/ ")
endif
#check if buf is installed
ifndef BUF_INSTALLED
$(error "Buf is not installed, please install buf - see https://docs.buf.build/installation")
endif
# install dependencies if not installed yet. see https://github.com/grpc-ecosystem/grpc-gateway#installation - versions are derived from go.mod via tools.go
ifndef GEN_GW_INSTALLED
@echo "Installing protoc grpc gateway plugin to gobin"
@go mod tidy && go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
endif
ifndef GEN_OPENAPI_INSTALLED
@echo "Installing protoc openapi plugin to gobin"
@go mod tidy && go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
endif
ifndef GEN_GO_GRPC_INSTALLED
@echo "Installing protoc go-grpc plugin to gobin"
@go mod tidy && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
endif
ifndef GEN_GO_INSTALLED
@echo "Installing protoc go plugin to gobin"
@go mod tidy && go install google.golang.org/protobuf/cmd/protoc-gen-go
endif
#run buf from api dir when everything is ok
@echo "Generating grpc gateway code from .proto files"
@cd api && buf generate
# generate go code and openAPI v2 spec, then converts the v2 spec to v3, then validates it.
.PHONY: apigen
apigen: buf-gen apigen-v3 apigen-validate
# remove all generated files
.PHONY: clean
clean: tls-delete apigen-v2-delete binary-delete
@echo "All generated artifacts removed."
# run go linter with the repositories lint config
.PHONY: lint
lint:
@echo ""
@echo "Linting code."
@$(DOCKER) run -t --rm -v $(PWD):/app -w /app golangci/golangci-lint golangci-lint run -v
# run short subset of tests
.PHONY: test-short
test-short:
$(GO) test -short $(PWD)/...
# run all tests
.PHONY: test
test:
@echo ""
@echo "Running tests."
@$(GO) test $(PWD)/... -count=1
# mimics the CI that runs on PR
.PHONY: pr-check
pr-check: gmtidy arch-check test lint binary
# runs go mod tidy
.PHONY: gmtidy
gmtidy:
@echo "Tidying dependencies using go mod tidy..."
@$(GO) mod tidy
# describes current architectural rules setup in arch-go.yml
.PHONY: arch-describe
arch-describe:
@echo ""
@echo "Current architecture rules:"
@$(DOCKER) run --rm -v $(PWD):/app -w /app quay.io/archgo/arch-go-test:latest describe
# checks if architectural rules are met
.PHONY: arch-check
arch-check:
@echo ""
@echo "Checking changes against architecture rules defined in arch-go.yml:"
@$(DOCKER) run --rm -v $(PWD):/app -w /app quay.io/archgo/arch-go-test:latest