Skip to content

Commit ab9a530

Browse files
committed
Introduce version subcommand
* Pull OIDC options into subcommands * Remove Docker based build, rely on GitHub Actions
1 parent 87bdd22 commit ab9a530

File tree

13 files changed

+124
-96
lines changed

13 files changed

+124
-96
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ jobs:
2121
steps:
2222
- name: Checkout
2323
uses: actions/checkout@v2
24+
with:
25+
fetch-depth: 0 # fetch also tag info, relevant for detecting the version
2426
- name: Setup go
2527
uses: actions/setup-go@v2
2628
with:
2729
go-version: 1.14.4
2830
- name: Build binary
29-
run: go build -v -ldflags="-s -w" ./cmd/oidc-token-ferry
31+
run: make
3032
- name: Compress binary
3133
uses: svenstaro/upx-action@v1-release
3234
with:

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
/oidc-token-ferry
2-
/oidc-token-ferry.darwin-amd64
3-
/oidc-token-ferry.linux-amd64
4-
/oidc-token-ferry.windows-amd64
2+
/oidc-token-ferry.exe

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Tom Wieczorek
3+
Copyright (c) 2018-2020 Tom Wieczorek
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
PROGRAM = oidc-token-ferry
22
GO_PACKAGE = github.com/twz123/$(PROGRAM)
3-
BUILDER_IMAGE = docker.io/golang:1.14.4-alpine3.12
43

54
# binaries
6-
DOCKER = docker
7-
GO = go
8-
DEP = dep
5+
GO = go
96

10-
OS_ARCH_PROGRAMS =
11-
PROGRAM_DEPENDENCIES = Makefile go.mod go.sum $(shell find pkg/ cmd/ -type f -name \*.go -print)
7+
VERSION := $(shell git describe --tags --always)
8+
GIT_UNTRACKEDCHANGES := $(shell git status --porcelain)
9+
ifneq ($(GIT_UNTRACKEDCHANGES),)
10+
VERSION := $(VERSION)-dirty
11+
endif
1212

13-
$(PROGRAM): $(PROGRAM_DEPENDENCIES)
14-
$(GO) build ./cmd/oidc-token-ferry
15-
16-
define _os_arch_program =
17-
OS_ARCH_PROGRAMS += $(PROGRAM).$(1)-$(2)
18-
oidc-token-ferry.$(1)-$(2): $(PROGRAM_DEPENDENCIES)
19-
$(DOCKER) run --rm -e GOOS=$(1) -e GOARCH=$(2) -e CGO_ENABLED=0 -v "$(shell pwd -P):/go/src/$(GO_PACKAGE):ro" -w "/go/src/$(GO_PACKAGE)/cmd/$(PROGRAM)" $(BUILDER_IMAGE) \
20-
sh -c 'go build -ldflags="-s -w" -o /tmp/go.out && apk add --no-cache upx 1>&2 && upx -o /tmp/go.out.upx /tmp/go.out 1>&2 && cat /tmp/go.out.upx' > $(PROGRAM).$(1)-$(2) || { rm $(PROGRAM).$(1)-$(2); exit 1; }
21-
chmod +x $(PROGRAM).$(1)-$(2)
22-
endef
23-
24-
$(eval $(call _os_arch_program,linux,amd64))
25-
$(eval $(call _os_arch_program,darwin,amd64))
26-
$(eval $(call _os_arch_program,windows,amd64))
27-
28-
.PHONY: all
29-
all: $(OS_ARCH_PROGRAMS)
30-
31-
.PHONY: clean
32-
clean:
33-
rm -f $(PROGRAM) $(OS_ARCH_PROGRAMS)
13+
.PHONY: build
14+
build: Makefile go.mod go.sum $(shell find pkg/ cmd/ -type f -name \*.go -print)
15+
$(GO) build -ldflags="-s -w -X $(GO_PACKAGE)/cmd/$(PROGRAM)/version.VERSION=$(VERSION)" ./cmd/$(PROGRAM)

README.md

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# OpenID Connect Token Ferry
22

3+
[![Build](https://github.com/twz123/oidc-token-ferry/workflows/Build/badge.svg)](https://github.com/twz123/oidc-token-ferry/actions?query=workflow%3ABuild)
4+
35
Performs an OpenID Connect Authentication Flow from the command line using an
46
"out of band" redirect URL. The OpenID Connect Issuer will provide a "code"
57
after the user has been authenticated. That code needs to be fed into this CLI.
@@ -11,55 +13,77 @@ solves this problem, but specifically for Google as Identity Provider.
1113

1214
## Usage
1315

14-
Usage:
15-
oidc-token-ferry [OPTIONS] <patch-kubeconfig | render-go-template | render-json>
16+
General usage:
1617

17-
OpenID Connect Options:
18-
-u, --issuer-url= IdP Issuer URL to be contacted (default: https://accounts.google.com)
19-
-i, --client-id= Client ID to be used
20-
-s, --client-secret= Client Secret to be used
21-
-r, --redirect-url= Redirect URL to be communicated to the IdP (needs to indicate "out of band") (default: urn:ietf:wg:oauth:2.0:oob)
22-
-c, --claim= Additional claims to be requested
18+
Usage:
19+
oidc-token-ferry [OPTIONS] <command>
2320

2421
Help Options:
25-
-h, --help Show this help message
22+
-h, --help Show this help message
2623

2724
Available commands:
2825
patch-kubeconfig patches Kubernetes kubeconfig files
2926
render-go-template renders credentials using Go Templates
3027
render-json renders credentials as JSON
28+
version Show oidc-token-ferry version information
29+
30+
How to patch a kubeconfig:
31+
32+
Usage:
33+
oidc-token-ferry [OPTIONS] patch-kubeconfig [patch-kubeconfig-OPTIONS] [KUBECONFIG_FILE] [OUTPUT_FILE]
34+
35+
Help Options:
36+
-h, --help Show this help message
3137

3238
[patch-kubeconfig command options]
3339
--user-name= User name to use when generating client configuration. Either user-name or user-claim-name may be specified.
3440
--user-claim-name= Claim that defines the user name to use when generating client configuration. Either user-name or user-claim-name may be specified.
41+
--no-open-url Don't open the redirect URL in a browser automatically
42+
43+
OpenID Connect Options:
44+
-u, --issuer-url= IdP Issuer URL to be contacted (default: https://accounts.google.com)
45+
-i, --client-id= Client ID to be used
46+
-s, --client-secret= Client Secret to be used
47+
-r, --redirect-url= Redirect URL to be communicated to the IdP (needs to indicate "out of band") (default: urn:ietf:wg:oauth:2.0:oob)
48+
-c, --claim= Additional claims to be requested
3549

3650
[patch-kubeconfig command arguments]
3751
KUBECONFIG_FILE: Path to the kubeconfig file to be patched. Uses the default discovery mechanism if omitted/empty. Special value '-' (hyphen) means read from STDIN.
3852
OUTPUT_FILE: Path to the patched kubeconfig file to be written. Overwrites kubeconfig if omitted/empty. Special value '-' (hyphen) means write to STDOUT.
3953

54+
How to render credentials via go-template:
55+
56+
Usage:
57+
oidc-token-ferry [OPTIONS] render-go-template [render-go-template-OPTIONS] TEMPLATE_STRING
58+
59+
Help Options:
60+
-h, --help Show this help message
61+
4062
[render-go-template command options]
4163
-o, --output-file= Output file to write (defaults to STDOUT if omitted)
64+
--no-open-url Don't open the redirect URL in a browser automatically
65+
66+
OpenID Connect Options:
67+
-u, --issuer-url= IdP Issuer URL to be contacted (default: https://accounts.google.com)
68+
-i, --client-id= Client ID to be used
69+
-s, --client-secret= Client Secret to be used
70+
-r, --redirect-url= Redirect URL to be communicated to the IdP (needs to indicate "out of band") (default: urn:ietf:wg:oauth:2.0:oob)
71+
-c, --claim= Additional claims to be requested
4272

4373
[render-go-template command arguments]
4474
TEMPLATE_STRING: Go Template to be rendered. An empty template indicates that the template is to be read from STDIN.
4575

46-
[render-json command options]
47-
-o, --output-file= Output file to write (defaults to STDOUT if omitted)
48-
4976
## Building
5077

51-
go build ./cmd/oidc-token-ferry
52-
53-
There's also a `Makefile` that'll build statically linked and compressed
54-
binaries for darwin/linux amd64 using Go 1.9.2 inside a Docker container:
78+
make
5579

56-
make all
80+
This will build a statically linked binary.
5781

5882
## License
5983

6084
MIT License
6185

62-
Copyright (c) 2018 Tom Wieczorek
86+
Copyright (c) 2018-2020 Tom Wieczorek
6387

6488
Permission is hereby granted, free of charge, to any person obtaining a copy
6589
of this software and associated documentation files (the "Software"), to deal

cmd/oidc-token-ferry/kubeconfig/patchcmd.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
const useStdInOut = "-"
1818

19-
type patchCmd struct {
19+
type PatchCmd struct {
2020
UserName func(string) `long:"user-name" description:"User name to use when generating client configuration. Either user-name or user-claim-name may be specified."`
2121
UserClaimName func(string) `long:"user-claim-name" description:"Claim that defines the user name to use when generating client configuration. Either user-name or user-claim-name may be specified."`
2222

@@ -25,20 +25,20 @@ type patchCmd struct {
2525
Outputconfig flags.Filename `positional-arg-name:"OUTPUT_FILE" description:"Path to the patched kubeconfig file to be written. Overwrites kubeconfig if omitted/empty. Special value '-' (hyphen) means write to STDOUT."`
2626
} `positional-args:"yes"`
2727

28-
cli cli.CLI
28+
cli.TokenFerryCmd
2929

3030
internalError error
3131
determineUserName func(*api.TokenFerry) (string, error)
3232
}
3333

34-
func PatchCmd(cli cli.CLI) interface{} {
35-
cmd := &patchCmd{cli: cli}
34+
func NewPatchCmd() *PatchCmd {
35+
cmd := &PatchCmd{}
3636
cmd.UserName = cmd.makeUserSelector(selectStaticUserName)
3737
cmd.UserClaimName = cmd.makeUserSelector(selectUserNameFromClaim)
3838
return cmd
3939
}
4040

41-
func (cmd *patchCmd) Execute([]string) error {
41+
func (cmd *PatchCmd) Execute([]string) error {
4242
if cmd.internalError != nil {
4343
return cmd.internalError
4444
}
@@ -55,7 +55,7 @@ func (cmd *patchCmd) Execute([]string) error {
5555
return errors.Wrap(err, "failed to patch kubeconfig")
5656
}
5757

58-
ferry, err := cmd.cli.PerformChallenge()
58+
ferry, err := cmd.TokenFerryCmd.PerformChallenge()
5959
if err != nil {
6060
return err
6161
}
@@ -136,7 +136,7 @@ func loadClientConfig(kubeconfig string) (*clientcmdapi.Config, string, error) {
136136
}
137137
}
138138

139-
func (cmd *patchCmd) makeUserSelector(selector func(*api.TokenFerry, string) (string, error)) func(string) {
139+
func (cmd *PatchCmd) makeUserSelector(selector func(*api.TokenFerry, string) (string, error)) func(string) {
140140
return func(value string) {
141141
if cmd.determineUserName != nil {
142142
cmd.internalError = errors.New("either user-name or user-claim-name may be specified")

cmd/oidc-token-ferry/main.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/twz123/oidc-token-ferry/cmd/oidc-token-ferry/kubeconfig"
99
"github.com/twz123/oidc-token-ferry/cmd/oidc-token-ferry/render"
10+
"github.com/twz123/oidc-token-ferry/cmd/oidc-token-ferry/version"
1011
)
1112

1213
const (
@@ -15,14 +16,15 @@ const (
1516
xCLIUsage
1617
)
1718

18-
func main() {
19-
cli := &tokenFerryCmd{}
20-
21-
parser := flags.NewParser(cli, flags.Default)
19+
type cli struct {
20+
VersionCmd version.VersionCmd `command:"version" description:"Show oidc-token-ferry version information"`
21+
JSONCmd render.JSONCmd `command:"render-json" description:"renders credentials as JSON"`
22+
GoTemplateCmd render.GoTemplateCmd `command:"render-go-template" description:"renders credentials using Go Templates"`
23+
PatchCmd *kubeconfig.PatchCmd `command:"patch-kubeconfig" description:"patches Kubernetes kubeconfig files"`
24+
}
2225

23-
cmd(parser, render.JsonCmd(cli), "render-json", "renders credentials as JSON")
24-
cmd(parser, render.GoTemplateCmd(cli), "render-go-template", "renders credentials using Go Templates")
25-
cmd(parser, kubeconfig.PatchCmd(cli), "patch-kubeconfig", "patches Kubernetes kubeconfig files")
26+
func main() {
27+
parser := flags.NewParser(&cli{PatchCmd: kubeconfig.NewPatchCmd()}, flags.Default)
2628

2729
_, err := parser.Parse()
2830
if err != nil {
@@ -39,9 +41,3 @@ func main() {
3941

4042
os.Exit(xOK)
4143
}
42-
43-
func cmd(parser *flags.Parser, data interface{}, name, desc string) {
44-
if _, err := parser.AddCommand(name, desc, "", data); err != nil {
45-
panic(err)
46-
}
47-
}

cmd/oidc-token-ferry/render/gotemplatecmd.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ import (
1111
"github.com/twz123/oidc-token-ferry/pkg/cli"
1212
)
1313

14-
type goTemplateCmd struct {
14+
type GoTemplateCmd struct {
1515
cli.OutputTarget
1616

1717
Args struct {
1818
TemplateString string `positional-arg-name:"TEMPLATE_STRING" description:"Go Template to be rendered. An empty template indicates that the template is to be read from STDIN."`
1919
} `positional-args:"yes" required:"yes"`
2020

21-
cli cli.CLI
21+
cli.TokenFerryCmd
2222
}
2323

24-
func GoTemplateCmd(cli cli.CLI) interface{} { return &goTemplateCmd{cli: cli} }
25-
26-
func (cmd *goTemplateCmd) Execute(args []string) error {
24+
func (cmd *GoTemplateCmd) Execute(args []string) error {
2725
template, err := cmd.parseTemplate()
2826
if err != nil {
2927
return err
3028
}
3129

32-
ferry, err := cmd.cli.PerformChallenge()
30+
ferry, err := cmd.TokenFerryCmd.PerformChallenge()
3331
if err != nil {
3432
return err
3533
}
@@ -39,7 +37,7 @@ func (cmd *goTemplateCmd) Execute(args []string) error {
3937
})
4038
}
4139

42-
func (cmd *goTemplateCmd) parseTemplate() (*template.Template, error) {
40+
func (cmd *GoTemplateCmd) parseTemplate() (*template.Template, error) {
4341
if cmd.Args.TemplateString == "" {
4442
return parseTemplateFromStdin()
4543
}

cmd/oidc-token-ferry/render/jsoncmd.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88
"github.com/twz123/oidc-token-ferry/pkg/cli"
99
)
1010

11-
type jsonCmd struct {
11+
type JSONCmd struct {
1212
cli.OutputTarget
13-
cli cli.CLI
13+
cli.TokenFerryCmd
1414
}
1515

16-
func JsonCmd(cli cli.CLI) interface{} { return &jsonCmd{cli: cli} }
17-
18-
func (cmd *jsonCmd) Execute(args []string) error {
19-
ferry, err := cmd.cli.PerformChallenge()
16+
func (cmd *JSONCmd) Execute(args []string) error {
17+
ferry, err := cmd.TokenFerryCmd.PerformChallenge()
2018
if err != nil {
2119
return err
2220
}

cmd/oidc-token-ferry/version/cmd.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
type Version struct {
9+
Version string
10+
GoVersion string
11+
GoCompiler string
12+
GoOs string
13+
GoArch string
14+
}
15+
16+
func NewVersion() Version {
17+
return Version{
18+
Version: VERSION,
19+
GoVersion: runtime.Compiler,
20+
GoOs: runtime.GOOS,
21+
GoArch: runtime.GOARCH,
22+
}
23+
}
24+
25+
type VersionCmd struct {
26+
}
27+
28+
func (cmd *VersionCmd) Execute(args []string) error {
29+
_, err := fmt.Printf("oidc-token-ferry: %+v\n", NewVersion())
30+
return err
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package version
2+
3+
// VERSION indicates which version of the binary is running.
4+
var VERSION string

pkg/cli/cli.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)