fix: Example client (#1492)

* start

* fix path

* all protos

* cleanup

* fix

* slash

* change grpc out

* message

* running example

* add befehl

* ignore generate client code

* fix readme

* management

* push

* hobla

* add i

* change i

* cleanup

* comments

* add tag name

* mod tidy
This commit is contained in:
Silvan 2021-03-30 10:58:31 +02:00 committed by GitHub
parent 5c5918e545
commit 264dc2eca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1498 additions and 0 deletions

3
.gitignore vendored
View File

@ -48,3 +48,6 @@ console/src/app/proto/generated/
pkg/grpc/*/*.pb.*
pkg/grpc/*/*.swagger.json
pkg/grpc/*/mock/*.mock.go
# examples
examples/client/zitadel/*

View File

@ -0,0 +1,25 @@
#! /bin/sh
if [ -n $1 ]; then
GO_MESSAGE_IMPORT=$1/zitadel/message
else
echo "need message import"
exit 3
fi
generate () {
protoc \
-I=/.tmp/protos \
-I=/go/src/github.com/caos/zitadel/pkg/grpc/message \
-I=/go/src/github.com/caos/zitadel/internal/protoc/protoc-gen-authoption \
-I=/go/src \
--go_opt=Mproto/message.proto=${GO_MESSAGE_IMPORT} \
--go_out /go/src \
--go-grpc_out /go/src \
$1/$2
}
generate /go/src/github.com/caos/zitadel/pkg/grpc/message/proto message.proto
generate /go/src/github.com/caos/zitadel/pkg/grpc/admin/proto admin.proto
generate /go/src/github.com/caos/zitadel/pkg/grpc/auth/proto auth.proto
generate /go/src/github.com/caos/zitadel/pkg/grpc/management/proto management.proto

View File

@ -0,0 +1,70 @@
#######################
## These steps set platform / arch type specific variables
#######################
FROM alpine AS arm64-base
ENV PROTOC_ARCH aarch_64
FROM alpine AS amd64-base
ENV PROTOC_ARCH x86_64
#######################
## This step downloads the protofiles, protoc and protoc-gen-grpc-web for later use
#######################
FROM ${BUILDARCH}-base as base
ARG TAG_NAME=master
RUN apk add tar curl git
WORKDIR /.tmp
RUN protoc_url="https://github.com/protocolbuffers/protobuf/releases/download/v3.13.0/protoc-3.13.0-linux-${PROTOC_ARCH}.zip" \
&& wget -O protoc ${protoc_url} \
&& unzip protoc
RUN curl https://raw.githubusercontent.com/envoyproxy/protoc-gen-validate/v0.4.1/validate/validate.proto --create-dirs -o validate/validate.proto \
&& curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/v1.14.6/protoc-gen-swagger/options/annotations.proto --create-dirs -o protoc-gen-swagger/options/annotations.proto \
&& curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/v1.14.6/protoc-gen-swagger/options/openapiv2.proto --create-dirs -o protoc-gen-swagger/options/openapiv2.proto \
&& curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto --create-dirs -o google/api/annotations.proto \
&& curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto --create-dirs -o google/api/http.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/empty.proto --create-dirs -o google/protobuf/empty.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/timestamp.proto --create-dirs -o google/protobuf/timestamp.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/descriptor.proto --create-dirs -o google/protobuf/descriptor.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/duration.proto --create-dirs -o google/protobuf/duration.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/any.proto --create-dirs -o google/protobuf/any.proto \
&& curl https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/struct.proto --create-dirs -o google/protobuf/struct.proto
WORKDIR /zitadel
RUN git clone -b ${TAG_NAME} https://github.com/caos/zitadel
#######################
## Go base build
## Speed up this step by mounting your local go mod pkg directory
#######################
FROM golang:1.15 as zitadel-client
ARG PROJECT_PATH
WORKDIR /go/src/github.com/caos/zitadel
# copy ZITADEL proto dependencies
COPY --from=base /.tmp /.tmp/protos
COPY --from=base /zitadel/zitadel .
COPY --from=base /.tmp/bin /usr/local/bin/
RUN go mod download
RUN go install \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger \
github.com/golang/protobuf/protoc-gen-go \
github.com/envoyproxy/protoc-gen-validate
RUN go get -u google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
RUN ./build/zitadel/generate-grpc-clients.sh ${PROJECT_PATH}
#######################
## prepare generated files for output
#######################
FROM scratch as zitadel-copy
ARG PROJECT_PATH
COPY --from=zitadel-client /go/src/github.com/caos/zitadel/pkg/grpc/admin/*.pb.go /zitadel/admin/
COPY --from=zitadel-client /go/src/github.com/caos/zitadel/pkg/grpc/auth/*.pb.go /zitadel/auth/
COPY --from=zitadel-client /go/src/github.com/caos/zitadel/pkg/grpc/management/*.pb.go /zitadel/management/
COPY --from=zitadel-client /go/src/${PROJECT_PATH}/zitadel/message/*.pb.go /zitadel/message/

28
examples/client/README.md Normal file
View File

@ -0,0 +1,28 @@
# Example
This guide describes how to generate clients to interact with ZITADEL.
ZITADEL decided to not check in generated files after v0.104.5.
As the go-sdk is not ready yet we recommend to to build the client in your own project.
## Requirements
- docker
## Generate client stub
### PROJECT_PATH
The PROJECT_PATH argument is needed for replacing imports in the generated files.
The path MUST represent the folder where the generated ZITADEL packages will reside in.
This replacement is needed for the message proto.
### TAG_NAME
It's recommended to clone a specific tag.
For example: TAG_NAME=v0.118.3
`DOCKER_BUILDKIT=1 docker build --target zitadel-copy -t zitadel:example --build-arg PROJECT_PATH=github.com/caos/zitadel/examples/client --build-arg TAG_NAME=master -f Dockerfile . -o .`

13
examples/client/go.mod Normal file
View File

@ -0,0 +1,13 @@
module github.com/caos/zitadel/examples/client
go 1.15
require (
github.com/caos/zitadel v0.118.2
github.com/envoyproxy/protoc-gen-validate v0.5.0
github.com/golang/protobuf v1.5.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
google.golang.org/genproto v0.0.0-20210325224202-eed09b1b5210
google.golang.org/grpc v1.36.1
google.golang.org/protobuf v1.26.0
)

1310
examples/client/go.sum Normal file

File diff suppressed because it is too large Load Diff

49
examples/client/main.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"context"
"crypto/x509"
"log"
"strings"
// the generated zitadel files for management api
pb "github.com/caos/zitadel/examples/client/zitadel/management"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
//zitadelAPI is the default zitadel api
const zitadelAPI = "api.zitadel.ch:443"
func main() {
conn, err := grpc.Dial(zitadelAPI, grpc.WithTransportCredentials(cert()))
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := pb.NewManagementServiceClient(conn)
//call ZITADEL. the response has no payload so we ignore the res
// the call was successful if no error responded
_, err = client.Healthz(context.TODO(), &empty.Empty{})
if err != nil {
log.Fatalln("call failed: ", err)
}
log.Println("call was successful")
}
//cert load default cert pool for tls
func cert() credentials.TransportCredentials {
ca, err := x509.SystemCertPool()
if err != nil {
log.Println("unable to load cert pool")
}
if ca == nil {
ca = x509.NewCertPool()
}
servernameWithoutPort := strings.Split(zitadelAPI, ":")[0]
return credentials.NewClientTLSFromCert(ca, servernameWithoutPort)
}