Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OAuth2 support #502

Merged
merged 46 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d19344a
add oauth2 client and login
Nov 5, 2023
4fa7485
unused func
Nov 5, 2023
eeb80fc
tidy up
Nov 5, 2023
86885b5
test-token
Nov 5, 2023
75c6331
check if the auth is the problem
Nov 5, 2023
ae03a80
fix auth test
Nov 5, 2023
ab7769b
renaming
Nov 5, 2023
511643c
remove git config
Nov 5, 2023
8dd7dc0
add token verification
Nov 6, 2023
6c7cfff
fixed auth tests
Nov 7, 2023
9b49538
do not require name and email
Nov 7, 2023
7ddb6c1
get email from subject
Nov 7, 2023
6c017b8
Set Auth on http.Client in root command
Nov 7, 2023
7e13bc7
Fix req ctx update and auth strategy and add tests
Nov 7, 2023
0bca149
Remove unused log import
Nov 7, 2023
b00002d
Log jwt subject in reqresp logger
Nov 7, 2023
3afb3a8
Add logs for jwt exit points to know what happens if something fails
Nov 7, 2023
ba8b8cf
Add test and fix code to ensure both hamctl token and jwt works
Nov 8, 2023
3a325aa
Use custom type for context value and add helpers
Nov 8, 2023
f66c4c0
Use request context
Nov 8, 2023
6a89639
Use command context
Nov 8, 2023
b84bcff
Add test of cache refresh handling
Nov 8, 2023
be6439a
change file permissions
Nov 9, 2023
bbfbaad
moved config to args
Nov 10, 2023
b599563
removed unused ham token
Nov 10, 2023
d871145
Fix client-id flag
Nov 12, 2023
d5ca505
Add log init in root command to support bad flags logging
Nov 12, 2023
7aa459d
Clean up client id command args
Nov 12, 2023
40bf713
Rever logging in root.go
Nov 12, 2023
1ade3ca
Accept context in NewVerifier
Nov 12, 2023
b933451
Rearrange jwt file a bit
Nov 12, 2023
2041626
Tidy go.mod
Nov 12, 2023
1df8fd9
Remove git config code
Nov 12, 2023
7fce8c2
Update make files with new arguments
Nov 12, 2023
c925d9e
Merge branch 'master' into hamctl-oauth2-support
Nov 13, 2023
28c9421
Add subject to log context
Nov 13, 2023
223852f
Remove newline
Nov 13, 2023
7a4d15d
Remove unused jwkFetchTimeout argument
Nov 13, 2023
f6742ee
handle all the dots in bearer token
hoeg Nov 13, 2023
316d22a
Merge branch 'master' into hamctl-oauth2-support
mahlunar Dec 1, 2023
6d5469e
change to release manager folder
Dec 5, 2023
bb2a8be
rename struct
Dec 5, 2023
c2225eb
toggle static token off if empty
Dec 5, 2023
1f1535f
moved to function
Dec 5, 2023
8320504
Merge branch 'master' into hamctl-oauth2-support
Dec 5, 2023
63f8eef
Merge branch 'master' into hamctl-oauth2-support
Dec 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ else
RELEASE_MANAGER_INTEGRATION_RABBITMQ_HOST=localhost go test -count=1 ./...
endif

HAMCTL_OAUTH_IDP_URL=https://idpurl
HAMCTL_OAUTH_CLIENT_ID=client-id
JWKS_URLS=https://jwksurls/v1/keys
JWT_AUDIENCE=audience
JWT_ISSUER=issuer

AUTH_TOKEN=test
SSH_PRIVATE_KEY=~/.ssh/github
Expand All @@ -87,7 +92,10 @@ SERVER_START=./dist/server start \
--log.development t \
--config-repo ${CONFIG_REPO} \
--user-mappings '${USER_MAPPINGS}' \
--policy-branch-restrictions '${BRANCH_RESTRICTIONS}'
--policy-branch-restrictions '${BRANCH_RESTRICTIONS}' \
--jwks-urls '${JWKS_URLS}' \
--jwt-audience '${JWT_AUDIENCE}' \
--jwt-issuer '${JWT_ISSUER}'

server-memory: build_server
$(SERVER_START) \
Expand All @@ -102,6 +110,20 @@ server-rabbitmq: build_server
--amqp-user ${AMQP_USER} \
--amqp-password ${AMQP_PASSWORD}

SERVER_URL=http://localhost:8080
DAEMON_OAUTH_IDP_URL=https://idpurl
DAEMON_OAUTH_CLIENT_ID=client-id
DAEMON_OAUTH_CLIENT_SECRET=secret

daemon: build_daemon
./dist/daemon start \
--release-manager-url '${SERVER_URL}' \
--environment local \
--kubeconfig '${KUBECONFIG}' \
--idp-url '${DAEMON_OAUTH_IDP_URL}' \
--client-id '${DAEMON_OAUTH_CLIENT_ID}' \
--client-secret '${DAEMON_OAUTH_CLIENT_SECRET}'

artifact-init:
USER_MAPPINGS="[email protected][email protected],[email protected][email protected]" ./dist/artifact init --slack-token ${SLACK_TOKEN} --artifact-id "master-deed62270f-854d930ecb" --name "lunar-way-product-service" --service "product" --git-author-name "Kasper Nissen" --git-author-email "[email protected]" --git-message "This is a test message" --git-committer-name "Bjørn Sørensen" --git-committer-email "[email protected]" --git-sha deed62270f24f1ca8cf2c19b505b2c88036e1b1c --git-branch master --url "https://bitbucket.org/LunarWay/lunar-way-product-service/commits/a05e314599a7c202724d46a009fcc0f493bce035" --ci-job-url "https://jenkins.corp.com/job/bitbucket/job/lunar-way-product-service/job/master/170/display/redirect"

Expand Down
8 changes: 6 additions & 2 deletions cmd/artifact/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ example_resources:
echo "$$FLUX_KUSTOMIZATION" > examples/prod/kustomization.yaml

RELEASE_MANAGER_URL=http://localhost:8080
RELEASE_MANAGER_AUTH_TOKEN=test
OAUTH_IDP_URL=https://idpurl
OAUTH_CLIENT_ID=id
OAUTH_CLIENT_SECRET=secret

test_push: example example_resources
./artifact push \
--root examples \
--http-base-url ${RELEASE_MANAGER_URL} \
--http-auth-token ${RELEASE_MANAGER_AUTH_TOKEN}
--client-id ${OAUTH_CLIENT_ID} \
--client-secret ${OAUTH_CLIENT_SECRET} \
--idp-url ${OAUTH_IDP_URL}
23 changes: 15 additions & 8 deletions cmd/artifact/command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ import (

func pushCommand(options *Options) *cobra.Command {
releaseManagerClient := httpinternal.Client{}

var idpURL, clientID, clientSecret string
command := &cobra.Command{
Use: "push",
Short: "push artifact to artifact repository",
RunE: func(c *cobra.Command, args []string) error {
var artifactID string
var err error
ctx := context.Background()
authenticator := httpinternal.NewClientAuthenticator(clientID, clientSecret, idpURL)
releaseManagerClient.Auth = &authenticator

if releaseManagerClient.Metadata.AuthToken != "" {
hoeg marked this conversation as resolved.
Show resolved Hide resolved
artifactID, err = flow.PushArtifactToReleaseManager(ctx, &releaseManagerClient, options.FileName, options.RootPath)
if err != nil {
return err
}
artifactID, err = flow.PushArtifactToReleaseManager(ctx, &releaseManagerClient, options.FileName, options.RootPath)
if err != nil {
return err
}

client, err := intslack.NewClient(slack.New(options.SlackToken), options.UserMappings, options.EmailSuffix)
if err != nil {
fmt.Printf("Error, not able to create Slack client in successful command: %v", err)
Expand All @@ -47,13 +48,19 @@ func pushCommand(options *Options) *cobra.Command {
},
}
command.Flags().StringVar(&releaseManagerClient.BaseURL, "http-base-url", os.Getenv("ARTIFACT_URL"), "address of the http release manager server")
command.Flags().StringVar(&releaseManagerClient.Metadata.AuthToken, "http-auth-token", "", "auth token for the http service")
command.Flags().StringVar(&idpURL, "idp-url", "", "the url of the identity provider")
command.Flags().StringVar(&clientID, "client-id", "", "client id of this application issued by the identity provider")
command.Flags().StringVar(&clientSecret, "client-secret", "", "the client secret")

// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("http-base-url")
//nolint:errcheck
command.MarkFlagRequired("http-auth-token")
command.MarkFlagRequired("idp-url")
//nolint:errcheck
command.MarkFlagRequired("client-id")
//nolint:errcheck
command.MarkFlagRequired("client-secret")
return command
}
15 changes: 14 additions & 1 deletion cmd/daemon/command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// 3. Detects CreateContainerConfigError, and fetches the message about the wrong config.
func StartDaemon() *cobra.Command {
var environment, kubeConfigPath string
var idpURL, clientID, clientSecret string
var moduloCrashReportNotif float64
var logConfiguration *log.Configuration

Expand All @@ -34,6 +35,9 @@ func StartDaemon() *cobra.Command {
logConfiguration.ParseFromEnvironmnet()
log.Init(logConfiguration)

authenticator := httpinternal.NewClientAuthenticator(clientID, clientSecret, idpURL)
client.Auth = &authenticator

exporter := &kubernetes.ReleaseManagerExporter{
Log: log.With("type", "k8s-exporter"),
Client: client,
Expand Down Expand Up @@ -98,15 +102,24 @@ func StartDaemon() *cobra.Command {
},
}
command.Flags().StringVar(&client.BaseURL, "release-manager-url", os.Getenv("RELEASE_MANAGER_ADDRESS"), "address of the release-manager, e.g. http://release-manager")
command.Flags().StringVar(&client.Metadata.AuthToken, "auth-token", os.Getenv("DAEMON_AUTH_TOKEN"), "token to be used to communicate with the release-manager")
command.Flags().DurationVar(&client.Timeout, "http-timeout", 20*time.Second, "HTTP request timeout")
command.Flags().StringVar(&environment, "environment", "", "environment where release-daemon is running")
command.Flags().StringVar(&kubeConfigPath, "kubeconfig", "", "path to kubeconfig file. If not specified, then daemon is expected to run inside kubernetes")
command.Flags().Float64Var(&moduloCrashReportNotif, "modulo-crash-report-notif", 5, "modulo for how often to report CrashLoopBackOff events")
command.Flags().StringVar(&idpURL, "idp-url", "", "the url of the identity provider")
command.Flags().StringVar(&clientID, "client-id", "", "client id of this application issued by the identity provider")
command.Flags().StringVar(&clientSecret, "client-secret", "", "the client secret")

// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("environment")
//nolint:errcheck
command.MarkFlagRequired("idp-url")
//nolint:errcheck
command.MarkFlagRequired("client-id")
//nolint:errcheck
command.MarkFlagRequired("client-secret")
logConfiguration = log.RegisterFlags(command)
return command
}
67 changes: 0 additions & 67 deletions cmd/hamctl/command/actions/config_mock.go

This file was deleted.

28 changes: 7 additions & 21 deletions cmd/hamctl/command/actions/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@ package actions
import (
"net/http"

"github.com/lunarway/release-manager/internal/git"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/lunarway/release-manager/internal/intent"
)

//go:generate moq -rm -out config_mock.go . GitConfigAPI
type GitConfigAPI interface {
CommitterDetails() (*git.CommitterDetails, error)
}

type ReleaseResult struct {
Response httpinternal.ReleaseResponse
Environment string
Error error
}

type ReleaseHttpClient struct {
gitConfigAPI GitConfigAPI
client *httpinternal.Client
client *httpinternal.Client
}

func NewReleaseHttpClient(gitConfigAPI GitConfigAPI, client *httpinternal.Client) *ReleaseHttpClient {
func NewReleaseHttpClient(client *httpinternal.Client) *ReleaseHttpClient {
return &ReleaseHttpClient{
gitConfigAPI: gitConfigAPI,
client: client,
client: client,
}
}

Expand All @@ -44,23 +36,17 @@ func (hc *ReleaseHttpClient) ReleaseArtifactID(service, environment string, arti
// environments.
func (hc *ReleaseHttpClient) ReleaseArtifactIDMultipleEnvironments(service string, environments []string, artifactID string, intent intent.Intent) ([]ReleaseResult, error) {
var results []ReleaseResult
committer, err := hc.gitConfigAPI.CommitterDetails()
if err != nil {
return nil, err
}
path, err := hc.client.URL("release")
if err != nil {
return nil, err
}
for _, environment := range environments {
var resp httpinternal.ReleaseResponse
err = hc.client.Do(http.MethodPost, path, httpinternal.ReleaseRequest{
Service: service,
Environment: environment,
ArtifactID: artifactID,
CommitterName: committer.Name,
CommitterEmail: committer.Email,
hoeg marked this conversation as resolved.
Show resolved Hide resolved
Intent: intent,
Service: service,
Environment: environment,
ArtifactID: artifactID,
Intent: intent,
}, &resp)

results = append(results, ReleaseResult{
Expand Down
67 changes: 0 additions & 67 deletions cmd/hamctl/command/config_mock.go

This file was deleted.

17 changes: 17 additions & 0 deletions cmd/hamctl/command/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package command

import (
"github.com/lunarway/release-manager/internal/http"
"github.com/spf13/cobra"
)

func Login(authenticator http.UserAuthenticator) *cobra.Command {
return &cobra.Command{
Use: "login",
Short: `Log into the configured IdP`,
Args: cobra.ExactArgs(0),
RunE: func(c *cobra.Command, args []string) error {
return authenticator.Login(c.Context())
},
}
}
6 changes: 3 additions & 3 deletions cmd/hamctl/command/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

func NewPolicy(client *http.Client, service *string, gitConfigAPI GitConfigAPI) *cobra.Command {
func NewPolicy(client *http.Client, service *string) *cobra.Command {
var command = &cobra.Command{
Use: "policy",
Short: "Manage release policies for services.",
Expand All @@ -28,8 +28,8 @@ func NewPolicy(client *http.Client, service *string, gitConfigAPI GitConfigAPI)
c.HelpFunc()(c, args)
},
}
command.AddCommand(policy.NewApply(client, service, gitConfigAPI))
command.AddCommand(policy.NewApply(client, service))
command.AddCommand(policy.NewList(client, service))
command.AddCommand(policy.NewDelete(client, service, gitConfigAPI))
command.AddCommand(policy.NewDelete(client, service))
return command
}
Loading