Skip to content

Commit bd94f86

Browse files
authored
Add API Token Request (#14)
1 parent 29f218f commit bd94f86

File tree

22 files changed

+838
-80
lines changed

22 files changed

+838
-80
lines changed

.github/workflows/golang.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- name: test
5454
run: |
5555
go install github.com/onsi/ginkgo/ginkgo@${{ env.GINKGO_VERSION }}
56-
GINKGO=ginkgo make test
56+
GINKGO=ginkgo make test-ci
5757
make coverage
5858
- name: Convert coverage to lcov
5959
uses: jandelgado/[email protected]

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
SHELL := bash
22

33
# Directory, where all required tools are located (absolute path required)
4-
TOOLS_DIR ?= $(shell cd tools && pwd)
4+
TOOLS_DIR ?= $(shell cd tools 2>/dev/null && pwd)
5+
HACK_DIR ?= $(shell cd hack 2>/dev/null && pwd)
56

67
VERSION ?= 0.0.1-local
78
KUBE_NAMESPACE ?= platform-monoskope-monoskope

cmd/monoctl/create/api_token.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2021 Monoskope Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package create
16+
17+
import (
18+
"context"
19+
"strings"
20+
"time"
21+
22+
"github.com/finleap-connect/monoctl/cmd/monoctl/flags"
23+
"github.com/finleap-connect/monoctl/internal/config"
24+
"github.com/finleap-connect/monoctl/internal/usecases"
25+
"github.com/finleap-connect/monoctl/internal/util"
26+
auth_util "github.com/finleap-connect/monoctl/internal/util/auth"
27+
apiGateway "github.com/finleap-connect/monoskope/pkg/api/gateway"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func NewCreateAPITokenCmd() *cobra.Command {
32+
var userId string
33+
var scopes []string
34+
validity := time.Hour * 24
35+
36+
cmd := &cobra.Command{
37+
Use: "api-token",
38+
Short: "Let m8 issue an API token.",
39+
Long: `Retrieve an API token issued by the m8 control plane.`,
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
configManager := config.NewLoaderFromExplicitFile(flags.ExplicitFile)
42+
43+
return auth_util.RetryOnAuthFail(cmd.Context(), configManager, func(ctx context.Context) error {
44+
return usecases.NewCreateAPITokenUsecase(configManager, userId, scopes, validity).Run(ctx)
45+
})
46+
},
47+
}
48+
flags := cmd.Flags()
49+
50+
flags.StringVarP(&userId, "user", "u", "", "Specify the name or UUID of the user for whom the token should be issued. If not a UUID it will be treated as username.")
51+
util.PanicOnError(cmd.MarkFlagRequired("user"))
52+
53+
avialbleScopes := make([]string, len(apiGateway.AuthorizationScope_name))
54+
for _, value := range apiGateway.AuthorizationScope_name {
55+
avialbleScopes[apiGateway.AuthorizationScope_value[value]] = value
56+
}
57+
scopesUsage := "Specify the scopes for which the token should be valid.\nAvailable scopes: " + strings.Join(avialbleScopes, ", ")
58+
flags.StringSliceVarP(&scopes, "scopes", "s", scopes, scopesUsage)
59+
util.PanicOnError(cmd.MarkFlagRequired("scopes"))
60+
61+
flags.DurationP("validity", "v", validity, "Specify the validity period of the token.")
62+
63+
return cmd
64+
}

cmd/monoctl/create/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func NewCreateCmd() *cobra.Command {
3232
cmd.AddCommand(NewCreateClusterCmd())
3333
cmd.AddCommand(NewCreateTenantCmd())
3434
cmd.AddCommand(NewCreateKubeConfigCmd())
35+
cmd.AddCommand(NewCreateAPITokenCmd())
3536

3637
return cmd
3738
}

go.mk

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ GO_MODULE_MONOSKOPE ?= github.com/finleap-connect/monoskope
33
GO_MODULE ?= github.com/finleap-connect/monoctl
44

55
GO ?= go
6+
GOGET ?= $(HACK_DIR)/goget-wrapper
67

78
GINKGO ?= $(TOOLS_DIR)/ginkgo
89
GINKO_VERSION ?= v1.16.4
910

1011
LINTER ?= $(TOOLS_DIR)/golangci-lint
11-
LINTER_VERSION ?= v1.36.0
12+
LINTER_VERSION ?= v1.39.0
1213

1314
MOCKGEN ?= $(TOOLS_DIR)/mockgen
1415
GOMOCK_VERSION ?= v1.5.0
@@ -41,41 +42,35 @@ mod: ## Do go mod tidy, download, verify
4142
vet: ## Do go ver
4243
$(GO) vet ./...
4344

44-
lint: ## Do golangci-lint
45-
$(LINTER) run -v --no-config --deadline=5m
46-
4745
go: mod vet lint test ## Do go mod / vet / lint /test
4846

4947
run: ## run monoctl, use `ARGS="get user"` to pass arguments
5048
$(GO) run -ldflags "$(LDFLAGS)" cmd/monoctl/*.go $(ARGS)
5149

5250
test: ## run all tests
53-
@find . -name '*.coverprofile' -exec rm {} \;
54-
$(GINKGO) -r -v -cover *
55-
@echo "mode: set" > ./monoctl.coverprofile
56-
@find ./internal -name "*.coverprofile" -exec cat {} \; | grep -v mode: | sort -r >> ./monoctl.coverprofile
57-
@find ./internal -name '*.coverprofile' -exec rm {} \;
51+
# https://onsi.github.io/ginkgo/#running-tests
52+
find . -name '*.coverprofile' -exec rm {} \;
53+
@$(GINKGO) -r -v -cover --failFast -requireSuite -covermode count -outputdir=$(BUILD_PATH) -coverprofile=monoctl.coverprofile
5854

59-
coverage: ## show test coverage
60-
@find . -name '*.coverprofile' -exec go tool cover -func {} \;
55+
test-ci: ## run all tests in CICD
56+
# https://onsi.github.io/ginkgo/#running-tests
57+
find . -name '*.coverprofile' -exec rm {} \;
58+
@$(GINKGO) -r -cover --failFast -requireSuite -covermode count -outputdir=$(BUILD_PATH) -coverprofile=monoctl.coverprofile
6159

62-
loc: ## show loc statistics
63-
@gocloc .
60+
coverage: ## print coverage from coverprofiles
61+
@go tool cover -func monoctl.coverprofile
6462

65-
ginkgo-get: ## download ginkgo
66-
$(shell $(TOOLS_DIR)/goget-wrapper github.com/onsi/ginkgo/ginkgo@$(GINKO_VERSION))
63+
ginkgo-get $(GINKGO):
64+
$(shell $(GOGET) github.com/onsi/ginkgo/ginkgo@$(GINKO_VERSION))
6765

68-
golangci-lint-get: ## download golangci-lint
69-
$(shell curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_DIR) $(LINTER_VERSION))
66+
golangci-lint-get $(LINTER):
67+
$(shell $(HACK_DIR)/golangci-lint.sh -b $(TOOLS_DIR) $(LINTER_VERSION))
7068

7169
gomock-get: ## download gomock
7270
$(shell $(TOOLS_DIR)/goget-wrapper github.com/golang/mock/mockgen@$(GOMOCK_VERSION))
7371

74-
ginkgo-clean: ## cleanup ginkgo
75-
rm -Rf $(TOOLS_DIR)/ginkgo
76-
77-
golangci-lint-clean: ## cleanup golangci-lint
78-
rm -Rf $(TOOLS_DIR)/golangci-lint
72+
lint: $(LINTER) ## go lint
73+
$(LINTER) run -v --no-config --deadline=5m
7974

8075
tools: golangci-lint-get ginkgo-get gomock-get ## Target to install all required tools into TOOLS_DIR
8176

@@ -107,3 +102,4 @@ rebuild-mocks: ## rebuild go mocks
107102
$(MOCKGEN) -package domain -destination test/mock/domain/tenant_client.go github.com/finleap-connect/monoskope/pkg/api/domain TenantClient,Tenant_GetAllClient
108103
$(MOCKGEN) -package domain -destination test/mock/domain/certificate_client.go github.com/finleap-connect/monoskope/pkg/api/domain CertificateClient
109104
$(MOCKGEN) -package domain -destination test/mock/gateway/cluster_auth_client.go github.com/finleap-connect/monoskope/pkg/api/gateway ClusterAuthClient
105+
$(MOCKGEN) -package domain -destination test/mock/gateway/api_token_client.go github.com/finleap-connect/monoskope/pkg/api/gateway APITokenClient

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
require (
66
github.com/briandowns/spinner v1.18.0
77
github.com/cenkalti/backoff v2.2.1+incompatible
8-
github.com/finleap-connect/monoskope v0.1.6-rc11
8+
github.com/finleap-connect/monoskope v0.3.0
99
github.com/golang/mock v1.6.0
1010
github.com/google/uuid v1.3.0
1111
github.com/kubism/testutil v0.1.0-alpha.2
@@ -17,7 +17,7 @@ require (
1717
github.com/zalando/go-keyring v0.1.1
1818
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
1919
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
20-
google.golang.org/grpc v1.43.0
20+
google.golang.org/grpc v1.42.0
2121
google.golang.org/protobuf v1.27.1
2222
gopkg.in/yaml.v2 v2.4.0
2323
k8s.io/apimachinery v0.21.1
@@ -31,6 +31,7 @@ require (
3131
github.com/cespare/xxhash/v2 v2.1.2 // indirect
3232
github.com/danieljoos/wincred v1.1.0 // indirect
3333
github.com/davecgh/go-spew v1.1.1 // indirect
34+
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
3435
github.com/evanphx/json-patch v4.9.0+incompatible // indirect
3536
github.com/fatih/color v1.13.0 // indirect
3637
github.com/fsnotify/fsnotify v1.5.1 // indirect
@@ -42,7 +43,7 @@ require (
4243
github.com/google/go-cmp v0.5.6 // indirect
4344
github.com/google/gofuzz v1.2.0 // indirect
4445
github.com/googleapis/gnostic v0.4.1 // indirect
45-
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
46+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
4647
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
4748
github.com/imdario/mergo v0.3.12 // indirect
4849
github.com/inconshreveable/mousetrap v1.0.0 // indirect
@@ -55,7 +56,6 @@ require (
5556
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5657
github.com/modern-go/reflect2 v1.0.2 // indirect
5758
github.com/nxadm/tail v1.4.8 // indirect
58-
github.com/opencontainers/runc v1.0.3 // indirect
5959
github.com/pkg/errors v0.9.1 // indirect
6060
github.com/prometheus/client_golang v1.11.0 // indirect
6161
github.com/prometheus/client_model v0.2.0 // indirect
@@ -65,7 +65,7 @@ require (
6565
go.uber.org/atomic v1.7.0 // indirect
6666
go.uber.org/multierr v1.6.0 // indirect
6767
go.uber.org/zap v1.17.0 // indirect
68-
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
68+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
6969
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
7070
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
7171
golang.org/x/text v0.3.7 // indirect

0 commit comments

Comments
 (0)