Skip to content

Commit

Permalink
Add API Token Request (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jastBytes authored Dec 22, 2021
1 parent 29f218f commit bd94f86
Show file tree
Hide file tree
Showing 22 changed files with 838 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- name: test
run: |
go install github.com/onsi/ginkgo/ginkgo@${{ env.GINKGO_VERSION }}
GINKGO=ginkgo make test
GINKGO=ginkgo make test-ci
make coverage
- name: Convert coverage to lcov
uses: jandelgado/[email protected]
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
SHELL := bash

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

VERSION ?= 0.0.1-local
KUBE_NAMESPACE ?= platform-monoskope-monoskope
Expand Down
64 changes: 64 additions & 0 deletions cmd/monoctl/create/api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2021 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package create

import (
"context"
"strings"
"time"

"github.com/finleap-connect/monoctl/cmd/monoctl/flags"
"github.com/finleap-connect/monoctl/internal/config"
"github.com/finleap-connect/monoctl/internal/usecases"
"github.com/finleap-connect/monoctl/internal/util"
auth_util "github.com/finleap-connect/monoctl/internal/util/auth"
apiGateway "github.com/finleap-connect/monoskope/pkg/api/gateway"
"github.com/spf13/cobra"
)

func NewCreateAPITokenCmd() *cobra.Command {
var userId string
var scopes []string
validity := time.Hour * 24

cmd := &cobra.Command{
Use: "api-token",
Short: "Let m8 issue an API token.",
Long: `Retrieve an API token issued by the m8 control plane.`,
RunE: func(cmd *cobra.Command, args []string) error {
configManager := config.NewLoaderFromExplicitFile(flags.ExplicitFile)

return auth_util.RetryOnAuthFail(cmd.Context(), configManager, func(ctx context.Context) error {
return usecases.NewCreateAPITokenUsecase(configManager, userId, scopes, validity).Run(ctx)
})
},
}
flags := cmd.Flags()

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.")
util.PanicOnError(cmd.MarkFlagRequired("user"))

avialbleScopes := make([]string, len(apiGateway.AuthorizationScope_name))
for _, value := range apiGateway.AuthorizationScope_name {
avialbleScopes[apiGateway.AuthorizationScope_value[value]] = value
}
scopesUsage := "Specify the scopes for which the token should be valid.\nAvailable scopes: " + strings.Join(avialbleScopes, ", ")
flags.StringSliceVarP(&scopes, "scopes", "s", scopes, scopesUsage)
util.PanicOnError(cmd.MarkFlagRequired("scopes"))

flags.DurationP("validity", "v", validity, "Specify the validity period of the token.")

return cmd
}
1 change: 1 addition & 0 deletions cmd/monoctl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewCreateCmd() *cobra.Command {
cmd.AddCommand(NewCreateClusterCmd())
cmd.AddCommand(NewCreateTenantCmd())
cmd.AddCommand(NewCreateKubeConfigCmd())
cmd.AddCommand(NewCreateAPITokenCmd())

return cmd
}
40 changes: 18 additions & 22 deletions go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ GO_MODULE_MONOSKOPE ?= github.com/finleap-connect/monoskope
GO_MODULE ?= github.com/finleap-connect/monoctl

GO ?= go
GOGET ?= $(HACK_DIR)/goget-wrapper

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

LINTER ?= $(TOOLS_DIR)/golangci-lint
LINTER_VERSION ?= v1.36.0
LINTER_VERSION ?= v1.39.0

MOCKGEN ?= $(TOOLS_DIR)/mockgen
GOMOCK_VERSION ?= v1.5.0
Expand Down Expand Up @@ -41,41 +42,35 @@ mod: ## Do go mod tidy, download, verify
vet: ## Do go ver
$(GO) vet ./...

lint: ## Do golangci-lint
$(LINTER) run -v --no-config --deadline=5m

go: mod vet lint test ## Do go mod / vet / lint /test

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

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

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

loc: ## show loc statistics
@gocloc .
coverage: ## print coverage from coverprofiles
@go tool cover -func monoctl.coverprofile

ginkgo-get: ## download ginkgo
$(shell $(TOOLS_DIR)/goget-wrapper github.com/onsi/ginkgo/ginkgo@$(GINKO_VERSION))
ginkgo-get $(GINKGO):
$(shell $(GOGET) github.com/onsi/ginkgo/ginkgo@$(GINKO_VERSION))

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

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

ginkgo-clean: ## cleanup ginkgo
rm -Rf $(TOOLS_DIR)/ginkgo

golangci-lint-clean: ## cleanup golangci-lint
rm -Rf $(TOOLS_DIR)/golangci-lint
lint: $(LINTER) ## go lint
$(LINTER) run -v --no-config --deadline=5m

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

Expand Down Expand Up @@ -107,3 +102,4 @@ rebuild-mocks: ## rebuild go mocks
$(MOCKGEN) -package domain -destination test/mock/domain/tenant_client.go github.com/finleap-connect/monoskope/pkg/api/domain TenantClient,Tenant_GetAllClient
$(MOCKGEN) -package domain -destination test/mock/domain/certificate_client.go github.com/finleap-connect/monoskope/pkg/api/domain CertificateClient
$(MOCKGEN) -package domain -destination test/mock/gateway/cluster_auth_client.go github.com/finleap-connect/monoskope/pkg/api/gateway ClusterAuthClient
$(MOCKGEN) -package domain -destination test/mock/gateway/api_token_client.go github.com/finleap-connect/monoskope/pkg/api/gateway APITokenClient
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/briandowns/spinner v1.18.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/finleap-connect/monoskope v0.1.6-rc11
github.com/finleap-connect/monoskope v0.3.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/kubism/testutil v0.1.0-alpha.2
Expand All @@ -17,7 +17,7 @@ require (
github.com/zalando/go-keyring v0.1.1
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.43.0
google.golang.org/grpc v1.42.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.21.1
Expand All @@ -31,6 +31,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/danieljoos/wincred v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
github.com/evanphx/json-patch v4.9.0+incompatible // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
Expand All @@ -42,7 +43,7 @@ require (
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.4.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand All @@ -55,7 +56,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opencontainers/runc v1.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -65,7 +65,7 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
Loading

0 comments on commit bd94f86

Please sign in to comment.