mirror of
https://github.com/int128/kubelogin.git
synced 2026-02-20 03:19:50 +00:00
Compare commits
148 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79d8056c35 | ||
|
|
4138991339 | ||
|
|
f650827a5f | ||
|
|
56904e15b1 | ||
|
|
dd05c11359 | ||
|
|
ce61e09acf | ||
|
|
e4057db5b5 | ||
|
|
bb288b69d3 | ||
|
|
e220267de5 | ||
|
|
391754e1ce | ||
|
|
10c7b6a84f | ||
|
|
2176105a91 | ||
|
|
3c79a614ff | ||
|
|
f6dec8e3db | ||
|
|
1ea9027677 | ||
|
|
cfa38455ab | ||
|
|
0412f6a1b0 | ||
|
|
afc7974b79 | ||
|
|
97b0bdd53c | ||
|
|
ebe5feaed2 | ||
|
|
4427bc7985 | ||
|
|
8643d695fe | ||
|
|
f0cff5a54b | ||
|
|
8237928af3 | ||
|
|
01b270755b | ||
|
|
e97b4de40b | ||
|
|
5063550468 | ||
|
|
d5989ca256 | ||
|
|
c508a1b717 | ||
|
|
e133ea8541 | ||
|
|
3f2e84a1ea | ||
|
|
7011f03094 | ||
|
|
6aef98cef7 | ||
|
|
93bb1d39b9 | ||
|
|
c8116e2eae | ||
|
|
f2de8dd987 | ||
|
|
915fb35bc8 | ||
|
|
51ccd70af3 | ||
|
|
c6df597fb0 | ||
|
|
ee78f6f735 | ||
|
|
6e484a2b89 | ||
|
|
8050db7e05 | ||
|
|
cd54ca0df0 | ||
|
|
45f83b0b0e | ||
|
|
51b7ca1600 | ||
|
|
83f85a9b53 | ||
|
|
d82c8a2dd1 | ||
|
|
072bee6992 | ||
|
|
5c07850a68 | ||
|
|
5c8c80f055 | ||
|
|
bc7bfabfb2 | ||
|
|
73112546de | ||
|
|
356f0d519d | ||
|
|
e84d29bc6b | ||
|
|
ae80ebf148 | ||
|
|
5942c82b5f | ||
|
|
a5f9c698ea | ||
|
|
40ef2c25b8 | ||
|
|
2422c46271 | ||
|
|
6ca0ee8013 | ||
|
|
9ac252667a | ||
|
|
000711f52e | ||
|
|
e465c4852b | ||
|
|
003badb0bc | ||
|
|
0873a193a5 | ||
|
|
5e80b1858e | ||
|
|
c816281657 | ||
|
|
0db49860f9 | ||
|
|
d70c9db036 | ||
|
|
675b5e5fff | ||
|
|
e3bfc321a2 | ||
|
|
b34d0fb32f | ||
|
|
4c61a71ed4 | ||
|
|
8a02ed0fb0 | ||
|
|
3485c5408e | ||
|
|
fb99977e98 | ||
|
|
39b441a7c2 | ||
|
|
cde5becf67 | ||
|
|
460b14a159 | ||
|
|
8436fe3494 | ||
|
|
9d2319ee2f | ||
|
|
75277378fc | ||
|
|
89a1046ce3 | ||
|
|
15d40413e4 | ||
|
|
8525ba5142 | ||
|
|
dbddd6a07f | ||
|
|
839877b45e | ||
|
|
99ed86e22e | ||
|
|
a78b746c29 | ||
|
|
187bbc203c | ||
|
|
d4b5e511bb | ||
|
|
33241b8721 | ||
|
|
b72cb63826 | ||
|
|
63fda1db0f | ||
|
|
da95fe470f | ||
|
|
4b08a49a51 | ||
|
|
9c74f3748b | ||
|
|
17e03f2abc | ||
|
|
ebef81f9d7 | ||
|
|
8d0d82fb71 | ||
|
|
b600e54a12 | ||
|
|
75317f88a1 | ||
|
|
5a794e8ceb | ||
|
|
1fe1ec4c20 | ||
|
|
7676ffbfab | ||
|
|
7e1e6a096b | ||
|
|
4d3d1c3b78 | ||
|
|
1ebdfc0e4f | ||
|
|
9c67c52b34 | ||
|
|
550396e1dd | ||
|
|
34f0578b59 | ||
|
|
604d118b68 | ||
|
|
91959e8a56 | ||
|
|
9b325a66a9 | ||
|
|
8b6257d60b | ||
|
|
d469df4978 | ||
|
|
3ae68df848 | ||
|
|
e8805f7a94 | ||
|
|
717da9d442 | ||
|
|
de176cfbaa | ||
|
|
9bf8a89577 | ||
|
|
a91c020f46 | ||
|
|
d4fb49613d | ||
|
|
64b1d52208 | ||
|
|
a298058e3f | ||
|
|
309e73d8c0 | ||
|
|
857d5dad88 | ||
|
|
455c920b65 | ||
|
|
afad46817a | ||
|
|
4f506b9f62 | ||
|
|
72bc19bc10 | ||
|
|
69bcb16e26 | ||
|
|
978a45bcf1 | ||
|
|
62b9a2158d | ||
|
|
974fc5c526 | ||
|
|
2c7d958efd | ||
|
|
16b15cd21b | ||
|
|
3213572180 | ||
|
|
b7bbcd44e1 | ||
|
|
7726ac6c51 | ||
|
|
adaeba4c24 | ||
|
|
e8acaa28b3 | ||
|
|
031f9fb81a | ||
|
|
8a7da83338 | ||
|
|
b776bac764 | ||
|
|
4bf77886a8 | ||
|
|
ea711f91b4 | ||
|
|
cfc6376f69 |
@@ -2,35 +2,38 @@ version: 2
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/golang:1.10
|
||||
working_directory: /go/src/github.com/int128/kubelogin
|
||||
- image: circleci/golang:1.12.3
|
||||
steps:
|
||||
- checkout
|
||||
- run: go get -v -t -d ./...
|
||||
- run: go get github.com/golang/lint/golint
|
||||
- run: golint
|
||||
- run: go build -v
|
||||
|
||||
release:
|
||||
docker:
|
||||
- image: circleci/golang:1.10
|
||||
working_directory: /go/src/github.com/int128/kubelogin
|
||||
steps:
|
||||
- checkout
|
||||
- run: go get -v -t -d ./...
|
||||
- run: go get github.com/mitchellh/gox
|
||||
- run: |
|
||||
mkdir -p ~/bin
|
||||
echo 'export PATH="$HOME/bin:$PATH"' >> $BASH_ENV
|
||||
- run: |
|
||||
curl -L -o ~/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubectl
|
||||
chmod +x ~/bin/kubectl
|
||||
- run: |
|
||||
curl -L -o ~/bin/ghcp https://github.com/int128/ghcp/releases/download/v1.3.0/ghcp_linux_amd64
|
||||
chmod +x ~/bin/ghcp
|
||||
- run: |
|
||||
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ~/bin v1.16.0
|
||||
- run: go get github.com/int128/goxzst
|
||||
- run: go get github.com/tcnksm/ghr
|
||||
- run: gox --osarch 'darwin/amd64 linux/amd64 windows/amd64 windows/386' -output 'dist/{{.Dir}}_{{.OS}}_{{.Arch}}'
|
||||
- run: ghr -u "$CIRCLE_PROJECT_USERNAME" -r "$CIRCLE_PROJECT_REPONAME" "$CIRCLE_TAG" dist
|
||||
- checkout
|
||||
# workaround for https://github.com/golang/go/issues/27925
|
||||
- run: sed -e '/^k8s.io\/client-go /d' -i go.sum
|
||||
- run: make check
|
||||
- run: bash <(curl -s https://codecov.io/bash)
|
||||
- run: make run
|
||||
- run: |
|
||||
if [ "$CIRCLE_TAG" ]; then
|
||||
make release
|
||||
fi
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
build:
|
||||
all:
|
||||
jobs:
|
||||
- build
|
||||
- release:
|
||||
- build:
|
||||
context: open-source
|
||||
filters:
|
||||
branches:
|
||||
ignore: /.*/
|
||||
tags:
|
||||
only: /.*/
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,2 +1,9 @@
|
||||
/.idea
|
||||
|
||||
/.kubeconfig*
|
||||
|
||||
/dist
|
||||
/coverage.out
|
||||
|
||||
/kubelogin
|
||||
/.kubeconfig
|
||||
/kubectl-oidc_login
|
||||
|
||||
51
DESIGN.md
Normal file
51
DESIGN.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Design of kubelogin
|
||||
|
||||
This explains design of kubelogin.
|
||||
|
||||
## Use cases
|
||||
|
||||
Kubelogin is a command line tool and designed to run as both a standalone command and a kubectl plugin.
|
||||
|
||||
It respects the following flags, commonly used in kubectl:
|
||||
|
||||
```
|
||||
--kubeconfig string Path to the kubeconfig file
|
||||
--context string The name of the kubeconfig context to use
|
||||
--user string The name of the kubeconfig user to use. Prior to --context
|
||||
--certificate-authority string Path to a cert file for the certificate authority
|
||||
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
|
||||
-v, --v int If set to 1 or greater, it shows debug log
|
||||
```
|
||||
|
||||
As well as it respects the environment variable `KUBECONFIG`.
|
||||
|
||||
|
||||
### Login by the command
|
||||
|
||||
TODO
|
||||
|
||||
### Wrap kubectl and login transparently
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
## Architecture
|
||||
|
||||
Kubelogin consists of the following layers:
|
||||
|
||||
- `usecases`: This provides the use-cases.
|
||||
- `adaptor`: This provides external access and converts objects between the use-cases and external system.
|
||||
|
||||
|
||||
### Use-cases
|
||||
|
||||
This provides the use-cases mentioned in the previous section.
|
||||
|
||||
This layer should not contain external access such as HTTP requests and system calls.
|
||||
|
||||
|
||||
### Adaptor
|
||||
|
||||
This provides external access such as command line interface and HTTP requests.
|
||||
|
||||
|
||||
40
Makefile
Normal file
40
Makefile
Normal file
@@ -0,0 +1,40 @@
|
||||
TARGET := kubelogin
|
||||
TARGET_PLUGIN := kubectl-oidc_login
|
||||
CIRCLE_TAG ?= HEAD
|
||||
LDFLAGS := -X main.version=$(CIRCLE_TAG)
|
||||
|
||||
.PHONY: check run diagram release clean
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
check:
|
||||
golangci-lint run
|
||||
$(MAKE) -C e2e_test/keys/testdata
|
||||
go test -v -race -cover -coverprofile=coverage.out ./...
|
||||
|
||||
$(TARGET): $(wildcard *.go)
|
||||
go build -o $@ -ldflags "$(LDFLAGS)"
|
||||
|
||||
$(TARGET_PLUGIN): $(TARGET)
|
||||
ln -sf $(TARGET) $@
|
||||
|
||||
run: $(TARGET_PLUGIN)
|
||||
-PATH=.:$(PATH) kubectl oidc-login --help
|
||||
|
||||
diagram: docs/authn.png
|
||||
|
||||
%.png: %.seqdiag
|
||||
seqdiag -a -f /Library/Fonts/Verdana.ttf $<
|
||||
|
||||
dist:
|
||||
VERSION=$(CIRCLE_TAG) goxzst -d dist/gh/ -o "$(TARGET)" -t "kubelogin.rb oidc-login.yaml" -- -ldflags "$(LDFLAGS)"
|
||||
mv dist/gh/kubelogin.rb dist/
|
||||
|
||||
release: dist
|
||||
ghr -u "$(CIRCLE_PROJECT_USERNAME)" -r "$(CIRCLE_PROJECT_REPONAME)" "$(CIRCLE_TAG)" dist/gh/
|
||||
ghcp -u "$(CIRCLE_PROJECT_USERNAME)" -r "homebrew-$(CIRCLE_PROJECT_REPONAME)" -m "$(CIRCLE_TAG)" -C dist/ kubelogin.rb
|
||||
|
||||
clean:
|
||||
-rm $(TARGET)
|
||||
-rm $(TARGET_PLUGIN)
|
||||
-rm -r dist/
|
||||
327
README.md
327
README.md
@@ -1,153 +1,276 @@
|
||||
# kubelogin [](https://circleci.com/gh/int128/kubelogin)
|
||||
# kubelogin [](https://circleci.com/gh/int128/kubelogin) [](https://goreportcard.com/report/github.com/int128/kubelogin)
|
||||
|
||||
`kubelogin` is a command to get an OpenID Connect (OIDC) token for `kubectl` authentication.
|
||||
This is a kubectl plugin for [Kubernetes OpenID Connect (OIDC) authentication](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens), also known as `kubectl oidc-login`.
|
||||
|
||||
In Kubernetes OIDC authentication, kubectl does not provide actual authentication and we need to manually set an ID token and refresh token to the kubeconfig.
|
||||
Kubelogin provides browser based authentication and writes an ID token and refresh token to the kubeconfig.
|
||||
|
||||
|
||||
## Getting Started
|
||||
|
||||
Download [the latest release](https://github.com/int128/kubelogin/releases) and save it as `/usr/local/bin/kubelogin`.
|
||||
|
||||
You have to configure `kubectl` to authenticate with OIDC.
|
||||
See the later section for details.
|
||||
You can install the latest release from [Homebrew](https://brew.sh/), [Krew](https://github.com/kubernetes-sigs/krew) or [GitHub Releases](https://github.com/int128/kubelogin/releases) as follows:
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials CLUSTER_NAME \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://keycloak.example.com/auth/realms/hello \
|
||||
--auth-provider-arg client-id=kubernetes \
|
||||
--auth-provider-arg client-secret=YOUR_CLIENT_SECRET
|
||||
# Homebrew
|
||||
brew tap int128/kubelogin
|
||||
brew install kubelogin
|
||||
|
||||
# Krew
|
||||
kubectl krew install oidc-login
|
||||
|
||||
# GitHub Releases
|
||||
curl -LO https://github.com/int128/kubelogin/releases/download/v1.12.0/kubelogin_linux_amd64.zip
|
||||
unzip kubelogin_linux_amd64.zip
|
||||
ln -s kubelogin kubectl-oidc_login
|
||||
```
|
||||
|
||||
Run `kubelogin`.
|
||||
You need to configure the OIDC provider, Kubernetes API server, kubectl authentication and role binding.
|
||||
See the following documents for more:
|
||||
|
||||
- [Getting Started with Keycloak](docs/keycloak.md)
|
||||
- [Getting Started with Google Identity Platform](docs/google.md)
|
||||
- [Team Operation](docs/team_ops.md)
|
||||
|
||||
|
||||
### Login by the command
|
||||
|
||||
Just run the command:
|
||||
|
||||
```sh
|
||||
kubelogin
|
||||
|
||||
# or run as a kubectl plugin
|
||||
kubectl oidc-login
|
||||
```
|
||||
|
||||
It automatically opens the browser and you can log in to the provider.
|
||||
|
||||
<img src="docs/keycloak-login.png" alt="keycloak-login" width="455" height="329">
|
||||
|
||||
After authentication, it writes an ID token and refresh token to the kubeconfig.
|
||||
|
||||
```
|
||||
% kubelogin
|
||||
2018/08/10 10:36:38 Reading .kubeconfig
|
||||
2018/08/10 10:36:38 Using current context: devops.hidetake.org
|
||||
2018/08/10 10:36:41 Open http://localhost:8000 for authorization
|
||||
2018/08/10 10:36:45 GET /
|
||||
2018/08/10 10:37:07 GET /?state=...&session_state=...&code=ey...
|
||||
2018/08/10 10:37:08 Updated .kubeconfig
|
||||
Open http://localhost:8000 for authentication
|
||||
You got a valid token until 2019-05-18 10:28:51 +0900 JST
|
||||
Updated ~/.kubeconfig
|
||||
```
|
||||
|
||||
Now your `~/.kube/config` looks like:
|
||||
|
||||
```yaml
|
||||
# ~/.kube/config (snip)
|
||||
users:
|
||||
- name: hello.k8s.local
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
idp-issuer-url: https://keycloak.example.com/auth/realms/hello
|
||||
client-id: kubernetes
|
||||
client-secret: YOUR_SECRET
|
||||
id-token: ey... # kubelogin will update ID token here
|
||||
refresh-token: ey... # kubelogin will update refresh token here
|
||||
name: oidc
|
||||
```
|
||||
|
||||
Make sure you can access to the Kubernetes cluster:
|
||||
Now you can access to the cluster.
|
||||
|
||||
```
|
||||
% kubectl version
|
||||
Client Version: version.Info{...}
|
||||
Server Version: version.Info{...}
|
||||
% kubectl get pods
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
echoserver-86c78fdccd-nzmd5 1/1 Running 0 26d
|
||||
```
|
||||
|
||||
If the ID token is valid, kubelogin does nothing.
|
||||
|
||||
```
|
||||
% kubelogin
|
||||
You already have a valid token until 2019-05-18 10:28:51 +0900 JST
|
||||
```
|
||||
|
||||
If the ID token has expired, kubelogin will refresh the token using the refresh token in the kubeconfig.
|
||||
If the refresh token has expired, kubelogin will proceed the authentication.
|
||||
|
||||
|
||||
### Wrap kubectl and login transparently
|
||||
|
||||
You can wrap kubectl to transparently login to the provider.
|
||||
|
||||
```sh
|
||||
alias kubectl='kubelogin exec -- kubectl'
|
||||
|
||||
# or run as a kubectl plugin
|
||||
alias kubectl='kubectl oidc-login exec -- kubectl'
|
||||
```
|
||||
|
||||
If the token expired, it updates the kubeconfig and executes kubectl.
|
||||
|
||||
```
|
||||
% kubectl get pods
|
||||
Open http://localhost:8000 for authentication
|
||||
You got a valid token until 2019-06-05 19:05:34 +0900 JST
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
echoserver-86c78fdccd-nzmd5 1/1 Running 0 26d
|
||||
```
|
||||
|
||||
If the ID token is valid, it just executes kubectl.
|
||||
|
||||
```
|
||||
% kubectl get pods
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
echoserver-86c78fdccd-nzmd5 1/1 Running 0 26d
|
||||
```
|
||||
|
||||
If the ID token has expired, kubelogin will refresh the token using the refresh token in the kubeconfig.
|
||||
If the refresh token has expired, kubelogin will proceed the authentication.
|
||||
|
||||
Kubelogin respects kubectl options passed to the extra arguments.
|
||||
For example, if you run `kubectl --kubeconfig .kubeconfig`,
|
||||
it will update `.kubeconfig` and execute kubectl.
|
||||
|
||||
If the current auth provider is not `oidc`, it just executes kubectl.
|
||||
|
||||
|
||||
## Configuration
|
||||
|
||||
You can set the following environment variable:
|
||||
This document is for the development version.
|
||||
If you are looking for a specific version, see [the release tags](https://github.com/int128/kubelogin/tags).
|
||||
|
||||
- `KUBECONFIG` - Path to the config. Defaults to `~/.kube/config`.
|
||||
Kubelogin supports the following options:
|
||||
|
||||
```
|
||||
Usage:
|
||||
kubelogin [flags]
|
||||
kubelogin [command]
|
||||
|
||||
## Prerequisite
|
||||
Examples:
|
||||
# Login to the provider using authorization code grant.
|
||||
kubelogin
|
||||
|
||||
You have to setup your OIDC identity provider and Kubernetes cluster.
|
||||
# Login to the provider using resource owner password credentials grant.
|
||||
kubelogin --username USERNAME --password PASSWORD
|
||||
|
||||
### 1. Setup OIDC Identity Provider
|
||||
# Wrap kubectl and login transparently
|
||||
alias kubectl='kubelogin exec -- kubectl'
|
||||
|
||||
This tutorial assumes you have created an OIDC client with the following:
|
||||
Available Commands:
|
||||
exec Login transparently and execute the kubectl command
|
||||
help Help about any command
|
||||
version Print the version information
|
||||
|
||||
- Issuer URL: `https://keycloak.example.com/auth/realms/hello`
|
||||
- Client ID: `kubernetes`
|
||||
- Client Secret: `YOUR_CLIENT_SECRET`
|
||||
- Allowed redirect URLs: `http://localhost:8000/`
|
||||
- Groups claim: `groups` (optional for group based access controll)
|
||||
|
||||
### 2. Setup Kubernetes API Server
|
||||
|
||||
Configure the Kubernetes API server allows your identity provider.
|
||||
|
||||
If you are using [kops](https://github.com/kubernetes/kops), `kops edit cluster` and append the following settings:
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
kubeAPIServer:
|
||||
oidcClientID: kubernetes
|
||||
oidcGroupsClaim: groups
|
||||
oidcIssuerURL: https://keycloak.example.com/auth/realms/hello
|
||||
Flags:
|
||||
--kubeconfig string Path to the kubeconfig file
|
||||
--context string The name of the kubeconfig context to use
|
||||
--user string The name of the kubeconfig user to use. Prior to --context
|
||||
--certificate-authority string Path to a cert file for the certificate authority
|
||||
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
|
||||
-v, --v int If set to 1 or greater, it shows debug log
|
||||
--listen-port ints Port to bind to the local server. If multiple ports are given, it will try the ports in order (default [8000,18000])
|
||||
--skip-open-browser If true, it does not open the browser on authentication
|
||||
--username string If set, perform the resource owner password credentials grant
|
||||
--password string If set, use the password instead of asking it
|
||||
-h, --help help for kubelogin
|
||||
```
|
||||
|
||||
### 3. Setup kubectl
|
||||
It supports the following keys of `auth-provider` in a kubeconfig.
|
||||
See [kubectl authentication](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#using-kubectl) for more.
|
||||
|
||||
Run the following command to configure `kubectl` to authenticate by your identity provider.
|
||||
Key | Direction | Value
|
||||
----|-----------|------
|
||||
`idp-issuer-url` | Read (Mandatory) | Issuer URL of the provider.
|
||||
`client-id` | Read (Mandatory) | Client ID of the provider.
|
||||
`client-secret` | Read (Mandatory) | Client Secret of the provider.
|
||||
`idp-certificate-authority` | Read | CA certificate path of the provider.
|
||||
`idp-certificate-authority-data` | Read | Base64 encoded CA certificate of the provider.
|
||||
`extra-scopes` | Read | Scopes to request to the provider (comma separated).
|
||||
`id-token` | Write | ID token got from the provider.
|
||||
`refresh-token` | Write | Refresh token got from the provider.
|
||||
|
||||
|
||||
### Kubeconfig
|
||||
|
||||
You can set path to the kubeconfig file by the option or the environment variable just like kubectl.
|
||||
It defaults to `~/.kube/config`.
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials CLUSTER_NAME \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://keycloak.example.com/auth/realms/hello \
|
||||
--auth-provider-arg client-id=kubernetes \
|
||||
--auth-provider-arg client-secret=YOUR_CLIENT_SECRET
|
||||
# by the option
|
||||
kubelogin --kubeconfig /path/to/kubeconfig
|
||||
|
||||
# by the environment variable
|
||||
KUBECONFIG="/path/to/kubeconfig1:/path/to/kubeconfig2" kubelogin
|
||||
```
|
||||
|
||||
In actual team operation, you can share the following config to your team members for easy setup.
|
||||
If you set multiple files, kubelogin will find the file which has the current authentication (i.e. `user` and `auth-provider`) and write a token to it.
|
||||
|
||||
```yaml
|
||||
#!/bin/sh
|
||||
CLUSTER_NAME="hello.k8s.local"
|
||||
|
||||
# Set the certificate
|
||||
mkdir -p "$HOME/.kube"
|
||||
cat > "$HOME/.kube/$CLUSTER_NAME.crt" <<EOF
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MII...
|
||||
-----END CERTIFICATE-----
|
||||
EOF
|
||||
### Authentication flows
|
||||
|
||||
# Set the cluster
|
||||
kubectl config set-cluster "$CLUSTER_NAME" \
|
||||
--server https://api-xxx.xxx.elb.amazonaws.com \
|
||||
--certificate-authority "$HOME/.kube/$CLUSTER_NAME.crt"
|
||||
#### Authorization code flow
|
||||
|
||||
# Set the credentials
|
||||
kubectl config set-credentials "$CLUSTER_NAME" \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://keycloak.example.com/auth/realms/hello \
|
||||
--auth-provider-arg client-id=kubernetes \
|
||||
--auth-provider-arg client-secret=YOUR_SECRET
|
||||
Kubelogin performs the authorization code flow by default.
|
||||
|
||||
# Set the context
|
||||
kubectl config set-context "$CLUSTER_NAME" --cluster "$CLUSTER_NAME" --user "$CLUSTER_NAME"
|
||||
It starts the local server at port 8000 or 18000 by default.
|
||||
You need to register the following redirect URIs to the provider:
|
||||
|
||||
# Set the current context
|
||||
kubectl config use-context "$CLUSTER_NAME"
|
||||
- `http://localhost:8000`
|
||||
- `http://localhost:18000` (used if port 8000 is already in use)
|
||||
|
||||
You can change the ports by the option:
|
||||
|
||||
```sh
|
||||
kubelogin --listen-port 12345 --listen-port 23456
|
||||
```
|
||||
|
||||
|
||||
#### Resource owner password credentials grant flow
|
||||
|
||||
As well as you can use the resource owner password credentials grant flow.
|
||||
Keycloak supports this flow but you need to explicitly enable the "Direct Access Grants" feature in the client settings.
|
||||
Most OIDC providers do not support this flow.
|
||||
|
||||
You can pass the username and password:
|
||||
|
||||
```
|
||||
% kubelogin --username USER --password PASS
|
||||
```
|
||||
|
||||
or use the password prompt:
|
||||
|
||||
```
|
||||
% kubelogin --username USER
|
||||
Password:
|
||||
```
|
||||
|
||||
|
||||
### Extra scopes
|
||||
|
||||
You can set the extra scopes to request to the provider by `extra-scopes` in the kubeconfig.
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials keycloak --auth-provider-arg extra-scopes=email
|
||||
```
|
||||
|
||||
Currently kubectl does not accept multiple scopes, so you need to edit the kubeconfig as like:
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials keycloak --auth-provider-arg extra-scopes=SCOPES
|
||||
sed -i '' -e s/SCOPES/email,profile/ $KUBECONFIG
|
||||
```
|
||||
|
||||
|
||||
### CA Certificates
|
||||
|
||||
You can use your self-signed certificates for the provider.
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials keycloak \
|
||||
--auth-provider-arg idp-certificate-authority=$HOME/.kube/keycloak-ca.pem
|
||||
```
|
||||
|
||||
|
||||
### HTTP Proxy
|
||||
|
||||
You can set the following environment variables if you are behind a proxy: `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`.
|
||||
See also [net/http#ProxyFromEnvironment](https://golang.org/pkg/net/http/#ProxyFromEnvironment).
|
||||
|
||||
|
||||
## Contributions
|
||||
|
||||
This is an open source software licensed under Apache License 2.0.
|
||||
Feel free to open issues and pull requests.
|
||||
Feel free to open issues and pull requests for improving code and documents.
|
||||
|
||||
### Build
|
||||
### Development
|
||||
|
||||
Go 1.12 or later is required.
|
||||
|
||||
```sh
|
||||
go get github.com/int128/kubelogin
|
||||
# Run lint and tests
|
||||
make check
|
||||
|
||||
# Compile and run the command
|
||||
make
|
||||
./kubelogin
|
||||
```
|
||||
|
||||
### Release
|
||||
|
||||
CircleCI publishes the build to GitHub. See [.circleci/config.yml](.circleci/config.yml).
|
||||
|
||||
180
adaptors/cmd/cmd.go
Normal file
180
adaptors/cmd/cmd.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Set provides an implementation and interface for Cmd.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Cmd), "*"),
|
||||
wire.Bind(new(adaptors.Cmd), new(*Cmd)),
|
||||
)
|
||||
|
||||
const examples = ` # Login to the provider using authorization code grant.
|
||||
%[1]s
|
||||
|
||||
# Login to the provider using resource owner password credentials grant.
|
||||
%[1]s --username USERNAME --password PASSWORD
|
||||
|
||||
# Wrap kubectl and login transparently
|
||||
alias kubectl='%[1]s exec -- kubectl'`
|
||||
|
||||
var defaultListenPort = []int{8000, 18000}
|
||||
|
||||
// Cmd provides interaction with command line interface (CLI).
|
||||
type Cmd struct {
|
||||
Login usecases.Login
|
||||
LoginAndExec usecases.LoginAndExec
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
// Run parses the command line arguments and executes the specified use-case.
|
||||
// It returns an exit code, that is 0 on success or 1 on error.
|
||||
func (cmd *Cmd) Run(ctx context.Context, args []string, version string) int {
|
||||
var exitCode int
|
||||
executable := filepath.Base(args[0])
|
||||
var o struct {
|
||||
kubectlOptions
|
||||
kubeloginOptions
|
||||
}
|
||||
rootCmd := cobra.Command{
|
||||
Use: executable,
|
||||
Short: "Login to the OpenID Connect provider and update the kubeconfig",
|
||||
Example: fmt.Sprintf(examples, executable),
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(*cobra.Command, []string) {
|
||||
cmd.Logger.SetLevel(adaptors.LogLevel(o.Verbose))
|
||||
in := usecases.LoginIn{
|
||||
KubeconfigFilename: o.Kubeconfig,
|
||||
KubeconfigContext: kubeconfig.ContextName(o.Context),
|
||||
KubeconfigUser: kubeconfig.UserName(o.User),
|
||||
CACertFilename: o.CertificateAuthority,
|
||||
SkipTLSVerify: o.SkipTLSVerify,
|
||||
ListenPort: o.ListenPort,
|
||||
SkipOpenBrowser: o.SkipOpenBrowser,
|
||||
Username: o.Username,
|
||||
Password: o.Password,
|
||||
}
|
||||
if err := cmd.Login.Do(ctx, in); err != nil {
|
||||
cmd.Logger.Printf("error: %s", err)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
o.kubectlOptions.register(rootCmd.Flags())
|
||||
o.kubeloginOptions.register(rootCmd.Flags())
|
||||
|
||||
execCmd := cobra.Command{
|
||||
Use: "exec [flags] -- kubectl [args]",
|
||||
Short: "Login transparently and execute the kubectl command",
|
||||
Args: func(execCmd *cobra.Command, args []string) error {
|
||||
if execCmd.ArgsLenAtDash() == -1 {
|
||||
return xerrors.Errorf("double dash is missing, please run as %s exec -- kubectl", executable)
|
||||
}
|
||||
if len(args) < 1 {
|
||||
return xerrors.New("too few arguments")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Run: func(execCmd *cobra.Command, args []string) {
|
||||
// parse the extra args and override the kubectl options
|
||||
f := pflag.NewFlagSet(execCmd.Name(), pflag.ContinueOnError)
|
||||
o.kubectlOptions.register(f)
|
||||
// ignore unknown flags and help flags (-h/--help)
|
||||
f.ParseErrorsWhitelist.UnknownFlags = true
|
||||
f.BoolP("help", "h", false, "ignore help flags")
|
||||
if err := f.Parse(args); err != nil {
|
||||
cmd.Logger.Debugf(1, "error while parsing the extra arguments: %s", err)
|
||||
}
|
||||
cmd.Logger.SetLevel(adaptors.LogLevel(o.Verbose))
|
||||
in := usecases.LoginAndExecIn{
|
||||
LoginIn: usecases.LoginIn{
|
||||
KubeconfigFilename: o.Kubeconfig,
|
||||
KubeconfigContext: kubeconfig.ContextName(o.Context),
|
||||
KubeconfigUser: kubeconfig.UserName(o.User),
|
||||
CACertFilename: o.CertificateAuthority,
|
||||
SkipTLSVerify: o.SkipTLSVerify,
|
||||
ListenPort: o.ListenPort,
|
||||
SkipOpenBrowser: o.SkipOpenBrowser,
|
||||
Username: o.Username,
|
||||
Password: o.Password,
|
||||
},
|
||||
Executable: args[0],
|
||||
Args: args[1:],
|
||||
}
|
||||
out, err := cmd.LoginAndExec.Do(ctx, in)
|
||||
if err != nil {
|
||||
cmd.Logger.Printf("error: %s", err)
|
||||
exitCode = 1
|
||||
return
|
||||
}
|
||||
exitCode = out.ExitCode
|
||||
},
|
||||
}
|
||||
o.kubeloginOptions.register(execCmd.Flags())
|
||||
rootCmd.AddCommand(&execCmd)
|
||||
|
||||
versionCmd := cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version information",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(*cobra.Command, []string) {
|
||||
cmd.Logger.Printf("%s version %s", executable, version)
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(&versionCmd)
|
||||
|
||||
rootCmd.SetArgs(args[1:])
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
cmd.Logger.Debugf(1, "error while parsing the arguments: %s", err)
|
||||
return 1
|
||||
}
|
||||
return exitCode
|
||||
}
|
||||
|
||||
// kubectlOptions represents kubectl specific options.
|
||||
type kubectlOptions struct {
|
||||
Kubeconfig string
|
||||
Context string
|
||||
User string
|
||||
CertificateAuthority string
|
||||
SkipTLSVerify bool
|
||||
Verbose int
|
||||
}
|
||||
|
||||
func (o *kubectlOptions) register(f *pflag.FlagSet) {
|
||||
f.SortFlags = false
|
||||
f.StringVar(&o.Kubeconfig, "kubeconfig", "", "Path to the kubeconfig file")
|
||||
f.StringVar(&o.Context, "context", "", "The name of the kubeconfig context to use")
|
||||
f.StringVar(&o.User, "user", "", "The name of the kubeconfig user to use. Prior to --context")
|
||||
f.StringVar(&o.CertificateAuthority, "certificate-authority", "", "Path to a cert file for the certificate authority")
|
||||
f.BoolVar(&o.SkipTLSVerify, "insecure-skip-tls-verify", false, "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure")
|
||||
f.IntVarP(&o.Verbose, "v", "v", 0, "If set to 1 or greater, it shows debug log")
|
||||
}
|
||||
|
||||
// kubeloginOptions represents application specific options.
|
||||
type kubeloginOptions struct {
|
||||
ListenPort []int
|
||||
SkipOpenBrowser bool
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (o *kubeloginOptions) register(f *pflag.FlagSet) {
|
||||
f.SortFlags = false
|
||||
f.IntSliceVar(&o.ListenPort, "listen-port", defaultListenPort, "Port to bind to the local server. If multiple ports are given, it will try the ports in order")
|
||||
f.BoolVar(&o.SkipOpenBrowser, "skip-open-browser", false, "If true, it does not open the browser on authentication")
|
||||
f.StringVar(&o.Username, "username", "", "If set, perform the resource owner password credentials grant")
|
||||
f.StringVar(&o.Password, "password", "", "If set, use the password instead of asking it")
|
||||
}
|
||||
268
adaptors/cmd/cmd_test.go
Normal file
268
adaptors/cmd/cmd_test.go
Normal file
@@ -0,0 +1,268 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/mock_adaptors"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/int128/kubelogin/usecases/mock_usecases"
|
||||
)
|
||||
|
||||
func TestCmd_Run(t *testing.T) {
|
||||
const executable = "kubelogin"
|
||||
const version = "HEAD"
|
||||
|
||||
t.Run("login/Defaults", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
|
||||
login := mock_usecases.NewMockLogin(ctrl)
|
||||
login.EXPECT().
|
||||
Do(ctx, usecases.LoginIn{
|
||||
ListenPort: defaultListenPort,
|
||||
})
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().SetLevel(adaptors.LogLevel(0))
|
||||
|
||||
cmd := Cmd{
|
||||
Login: login,
|
||||
Logger: logger,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable}, version)
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exitCode wants 0 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("login/FullOptions", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
|
||||
login := mock_usecases.NewMockLogin(ctrl)
|
||||
login.EXPECT().
|
||||
Do(ctx, usecases.LoginIn{
|
||||
KubeconfigFilename: "/path/to/kubeconfig",
|
||||
KubeconfigContext: "hello.k8s.local",
|
||||
KubeconfigUser: "google",
|
||||
CACertFilename: "/path/to/cacert",
|
||||
SkipTLSVerify: true,
|
||||
ListenPort: []int{10080, 20080},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
})
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().SetLevel(adaptors.LogLevel(1))
|
||||
|
||||
cmd := Cmd{
|
||||
Login: login,
|
||||
Logger: logger,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable,
|
||||
"--kubeconfig", "/path/to/kubeconfig",
|
||||
"--context", "hello.k8s.local",
|
||||
"--user", "google",
|
||||
"--certificate-authority", "/path/to/cacert",
|
||||
"--insecure-skip-tls-verify",
|
||||
"-v1",
|
||||
"--listen-port", "10080",
|
||||
"--listen-port", "20080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER",
|
||||
"--password", "PASS",
|
||||
}, version)
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exitCode wants 0 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("login/TooManyArgs", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
cmd := Cmd{
|
||||
Login: mock_usecases.NewMockLogin(ctrl),
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
exitCode := cmd.Run(context.TODO(), []string{executable, "some"}, version)
|
||||
if exitCode != 1 {
|
||||
t.Errorf("exitCode wants 1 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("loginAndExec/Defaults", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
|
||||
loginAndExec := mock_usecases.NewMockLoginAndExec(ctrl)
|
||||
loginAndExec.EXPECT().
|
||||
Do(ctx, usecases.LoginAndExecIn{
|
||||
LoginIn: usecases.LoginIn{
|
||||
ListenPort: defaultListenPort,
|
||||
},
|
||||
Executable: "kubectl",
|
||||
Args: []string{"dummy"},
|
||||
}).
|
||||
Return(&usecases.LoginAndExecOut{ExitCode: 0}, nil)
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().SetLevel(adaptors.LogLevel(0))
|
||||
|
||||
cmd := Cmd{
|
||||
LoginAndExec: loginAndExec,
|
||||
Logger: logger,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable, "exec", "--", "kubectl", "dummy"}, version)
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exitCode wants 0 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("loginAndExec/OptionsInExtraArgs", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
|
||||
loginAndExec := mock_usecases.NewMockLoginAndExec(ctrl)
|
||||
loginAndExec.EXPECT().
|
||||
Do(ctx, usecases.LoginAndExecIn{
|
||||
LoginIn: usecases.LoginIn{
|
||||
KubeconfigFilename: "/path/to/kubeconfig2",
|
||||
KubeconfigContext: "hello2.k8s.local",
|
||||
KubeconfigUser: "google2",
|
||||
CACertFilename: "/path/to/cacert2",
|
||||
SkipTLSVerify: true,
|
||||
ListenPort: defaultListenPort,
|
||||
},
|
||||
Executable: "kubectl",
|
||||
Args: []string{
|
||||
"--kubeconfig", "/path/to/kubeconfig2",
|
||||
"--context", "hello2.k8s.local",
|
||||
"--user", "google2",
|
||||
"--certificate-authority", "/path/to/cacert2",
|
||||
"--insecure-skip-tls-verify",
|
||||
"-v2",
|
||||
"--listen-port", "30080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER2",
|
||||
"--password", "PASS2",
|
||||
"dummy",
|
||||
"--dummy",
|
||||
"--help",
|
||||
},
|
||||
}).
|
||||
Return(&usecases.LoginAndExecOut{ExitCode: 0}, nil)
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().SetLevel(adaptors.LogLevel(2))
|
||||
|
||||
cmd := Cmd{
|
||||
LoginAndExec: loginAndExec,
|
||||
Logger: logger,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable,
|
||||
"exec",
|
||||
"--",
|
||||
"kubectl",
|
||||
// kubectl options in the extra args should be mapped to the options
|
||||
"--kubeconfig", "/path/to/kubeconfig2",
|
||||
"--context", "hello2.k8s.local",
|
||||
"--user", "google2",
|
||||
"--certificate-authority", "/path/to/cacert2",
|
||||
"--insecure-skip-tls-verify",
|
||||
"-v2",
|
||||
// kubelogin options in the extra args should not affect
|
||||
"--listen-port", "30080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER2",
|
||||
"--password", "PASS2",
|
||||
"dummy",
|
||||
"--dummy",
|
||||
"--help",
|
||||
}, version)
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exitCode wants 0 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("loginAndExec/OverrideOptions", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
|
||||
loginAndExec := mock_usecases.NewMockLoginAndExec(ctrl)
|
||||
loginAndExec.EXPECT().
|
||||
Do(ctx, usecases.LoginAndExecIn{
|
||||
LoginIn: usecases.LoginIn{
|
||||
KubeconfigFilename: "/path/to/kubeconfig2",
|
||||
KubeconfigContext: "hello2.k8s.local",
|
||||
KubeconfigUser: "google2",
|
||||
CACertFilename: "/path/to/cacert2",
|
||||
SkipTLSVerify: true,
|
||||
ListenPort: []int{10080, 20080},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
},
|
||||
Executable: "kubectl",
|
||||
Args: []string{
|
||||
"--kubeconfig", "/path/to/kubeconfig2",
|
||||
"--context", "hello2.k8s.local",
|
||||
"--user", "google2",
|
||||
"--certificate-authority", "/path/to/cacert2",
|
||||
"--insecure-skip-tls-verify",
|
||||
"-v2",
|
||||
"--listen-port", "30080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER2",
|
||||
"--password", "PASS2",
|
||||
"dummy",
|
||||
"--dummy",
|
||||
},
|
||||
}).
|
||||
Return(&usecases.LoginAndExecOut{ExitCode: 0}, nil)
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().SetLevel(adaptors.LogLevel(2))
|
||||
|
||||
cmd := Cmd{
|
||||
LoginAndExec: loginAndExec,
|
||||
Logger: logger,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable,
|
||||
// kubelogin options in the first args should be mapped to the options
|
||||
"--listen-port", "10080",
|
||||
"--listen-port", "20080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER",
|
||||
"--password", "PASS",
|
||||
"exec",
|
||||
"--",
|
||||
"kubectl",
|
||||
// kubectl options in the extra args should be mapped to the options
|
||||
"--kubeconfig", "/path/to/kubeconfig2",
|
||||
"--context", "hello2.k8s.local",
|
||||
"--user", "google2",
|
||||
"--certificate-authority", "/path/to/cacert2",
|
||||
"--insecure-skip-tls-verify",
|
||||
"-v2",
|
||||
// kubelogin options in the extra args should not affect
|
||||
"--listen-port", "30080",
|
||||
"--skip-open-browser",
|
||||
"--username", "USER2",
|
||||
"--password", "PASS2",
|
||||
"dummy",
|
||||
"--dummy",
|
||||
}, version)
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exitCode wants 0 but %d", exitCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
54
adaptors/env/env.go
vendored
Normal file
54
adaptors/env/env.go
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Set provides an implementation and interface for Env.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Env), "*"),
|
||||
wire.Bind(new(adaptors.Env), new(*Env)),
|
||||
)
|
||||
|
||||
// Env provides environment specific facilities.
|
||||
type Env struct{}
|
||||
|
||||
// ReadPassword reads a password from the stdin without echo back.
|
||||
func (*Env) ReadPassword(prompt string) (string, error) {
|
||||
if _, err := fmt.Fprint(os.Stderr, "Password: "); err != nil {
|
||||
return "", xerrors.Errorf("could not write the prompt: %w", err)
|
||||
}
|
||||
b, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("could not read: %w", err)
|
||||
}
|
||||
if _, err := fmt.Fprintln(os.Stderr); err != nil {
|
||||
return "", xerrors.Errorf("could not write a new line: %w", err)
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// Exec executes the command and returns the exit code.
|
||||
// Unlike the exec package, this does not return an error even if the command exited with non-zero code.
|
||||
func (*Env) Exec(ctx context.Context, executable string, args []string) (int, error) {
|
||||
c := exec.CommandContext(ctx, executable, args...)
|
||||
c.Stdin = os.Stdin
|
||||
c.Stdout = os.Stdout
|
||||
c.Stderr = os.Stderr
|
||||
if err := c.Run(); err != nil {
|
||||
if err, ok := err.(*exec.ExitError); ok {
|
||||
return err.ExitCode(), nil
|
||||
}
|
||||
return 0, xerrors.Errorf("could not execute the command: %w", err)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
97
adaptors/interfaces.go
Normal file
97
adaptors/interfaces.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package adaptors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
)
|
||||
|
||||
//go:generate mockgen -destination mock_adaptors/mock_adaptors.go github.com/int128/kubelogin/adaptors Kubeconfig,OIDC,OIDCClient,Env,Logger
|
||||
|
||||
type Cmd interface {
|
||||
Run(ctx context.Context, args []string, version string) int
|
||||
}
|
||||
|
||||
type Kubeconfig interface {
|
||||
GetCurrentAuthProvider(explicitFilename string, contextName kubeconfig.ContextName, userName kubeconfig.UserName) (*kubeconfig.AuthProvider, error)
|
||||
UpdateAuthProvider(auth *kubeconfig.AuthProvider) error
|
||||
}
|
||||
|
||||
type OIDC interface {
|
||||
New(ctx context.Context, config OIDCClientConfig) (OIDCClient, error)
|
||||
}
|
||||
|
||||
// OIDCClientConfig represents a configuration of an OIDCClient to create.
|
||||
type OIDCClientConfig struct {
|
||||
Config kubeconfig.OIDCConfig
|
||||
CACertFilename string
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
type OIDCClient interface {
|
||||
AuthenticateByCode(ctx context.Context, in OIDCAuthenticateByCodeIn) (*OIDCAuthenticateOut, error)
|
||||
AuthenticateByPassword(ctx context.Context, in OIDCAuthenticateByPasswordIn) (*OIDCAuthenticateOut, error)
|
||||
Verify(ctx context.Context, in OIDCVerifyIn) (*OIDCVerifyOut, error)
|
||||
Refresh(ctx context.Context, in OIDCRefreshIn) (*OIDCAuthenticateOut, error)
|
||||
}
|
||||
|
||||
// OIDCAuthenticateByCodeIn represents an input DTO of OIDCClient.AuthenticateByCode.
|
||||
type OIDCAuthenticateByCodeIn struct {
|
||||
LocalServerPort []int // HTTP server port candidates
|
||||
SkipOpenBrowser bool // skip opening browser if true
|
||||
ShowLocalServerURL interface{ ShowLocalServerURL(url string) }
|
||||
}
|
||||
|
||||
// OIDCAuthenticateByPasswordIn represents an input DTO of OIDCClient.AuthenticateByPassword.
|
||||
type OIDCAuthenticateByPasswordIn struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
// OIDCAuthenticateOut represents an output DTO of
|
||||
// OIDCClient.AuthenticateByCode, OIDCClient.AuthenticateByPassword and OIDCClient.Refresh.
|
||||
type OIDCAuthenticateOut struct {
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
IDTokenExpiry time.Time
|
||||
IDTokenClaims map[string]string // string representation of claims for logging
|
||||
}
|
||||
|
||||
// OIDCVerifyIn represents an input DTO of OIDCClient.Verify.
|
||||
type OIDCVerifyIn struct {
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
}
|
||||
|
||||
// OIDCVerifyIn represents an output DTO of OIDCClient.Verify.
|
||||
type OIDCVerifyOut struct {
|
||||
IDTokenExpiry time.Time
|
||||
IDTokenClaims map[string]string // string representation of claims for logging
|
||||
}
|
||||
|
||||
// OIDCRefreshIn represents an input DTO of OIDCClient.Refresh.
|
||||
type OIDCRefreshIn struct {
|
||||
RefreshToken string
|
||||
}
|
||||
|
||||
type Env interface {
|
||||
ReadPassword(prompt string) (string, error)
|
||||
Exec(ctx context.Context, executable string, args []string) (int, error)
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
Printf(format string, v ...interface{})
|
||||
Debugf(level LogLevel, format string, v ...interface{})
|
||||
SetLevel(level LogLevel)
|
||||
IsEnabled(level LogLevel) bool
|
||||
}
|
||||
|
||||
// LogLevel represents a log level for debug.
|
||||
//
|
||||
// 0 = None
|
||||
// 1 = Including in/out
|
||||
// 2 = Including transport headers
|
||||
// 3 = Including transport body
|
||||
//
|
||||
type LogLevel int
|
||||
14
adaptors/kubeconfig/kubeconfig.go
Normal file
14
adaptors/kubeconfig/kubeconfig.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
)
|
||||
|
||||
// Set provides an implementation and interface for Kubeconfig.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Kubeconfig), "*"),
|
||||
wire.Bind(new(adaptors.Kubeconfig), new(*Kubeconfig)),
|
||||
)
|
||||
|
||||
type Kubeconfig struct{}
|
||||
85
adaptors/kubeconfig/load.go
Normal file
85
adaptors/kubeconfig/load.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"golang.org/x/xerrors"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
func (*Kubeconfig) GetCurrentAuthProvider(explicitFilename string, contextName kubeconfig.ContextName, userName kubeconfig.UserName) (*kubeconfig.AuthProvider, error) {
|
||||
config, err := loadByDefaultRules(explicitFilename)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not load kubeconfig: %w", err)
|
||||
}
|
||||
auth, err := findCurrentAuthProvider(config, contextName, userName)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not find the current auth provider: %w", err)
|
||||
}
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
func loadByDefaultRules(explicitFilename string) (*api.Config, error) {
|
||||
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
rules.ExplicitPath = explicitFilename
|
||||
config, err := rules.Load()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("error while loading config: %w", err)
|
||||
}
|
||||
return config, err
|
||||
}
|
||||
|
||||
// findCurrentAuthProvider resolves the current auth provider.
|
||||
// If contextName is given, this returns the user of the context.
|
||||
// If userName is given, this ignores the context and returns the user.
|
||||
// If any context or user is not found, this returns an error.
|
||||
func findCurrentAuthProvider(config *api.Config, contextName kubeconfig.ContextName, userName kubeconfig.UserName) (*kubeconfig.AuthProvider, error) {
|
||||
if userName == "" {
|
||||
if contextName == "" {
|
||||
contextName = kubeconfig.ContextName(config.CurrentContext)
|
||||
}
|
||||
contextNode, ok := config.Contexts[string(contextName)]
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("context %s does not exist", contextName)
|
||||
}
|
||||
userName = kubeconfig.UserName(contextNode.AuthInfo)
|
||||
}
|
||||
userNode, ok := config.AuthInfos[string(userName)]
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("user %s does not exist", userName)
|
||||
}
|
||||
if userNode.AuthProvider == nil {
|
||||
return nil, xerrors.New("auth-provider is missing")
|
||||
}
|
||||
if userNode.AuthProvider.Name != "oidc" {
|
||||
return nil, xerrors.Errorf("auth-provider.name must be oidc but is %s", userNode.AuthProvider.Name)
|
||||
}
|
||||
if userNode.AuthProvider.Config == nil {
|
||||
return nil, xerrors.New("auth-provider.config is missing")
|
||||
}
|
||||
return &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: userNode.LocationOfOrigin,
|
||||
UserName: userName,
|
||||
ContextName: contextName,
|
||||
OIDCConfig: makeOIDCConfig(userNode.AuthProvider.Config),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func makeOIDCConfig(m map[string]string) kubeconfig.OIDCConfig {
|
||||
var extraScopes []string
|
||||
if m["extra-scopes"] != "" {
|
||||
extraScopes = strings.Split(m["extra-scopes"], ",")
|
||||
}
|
||||
return kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: m["idp-issuer-url"],
|
||||
ClientID: m["client-id"],
|
||||
ClientSecret: m["client-secret"],
|
||||
IDPCertificateAuthority: m["idp-certificate-authority"],
|
||||
IDPCertificateAuthorityData: m["idp-certificate-authority-data"],
|
||||
ExtraScopes: extraScopes,
|
||||
IDToken: m["id-token"],
|
||||
RefreshToken: m["refresh-token"],
|
||||
}
|
||||
}
|
||||
225
adaptors/kubeconfig/load_test.go
Normal file
225
adaptors/kubeconfig/load_test.go
Normal file
@@ -0,0 +1,225 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/go-test/deep"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
func Test_loadByDefaultRules(t *testing.T) {
|
||||
t.Run("google.yaml>keycloak.yaml", func(t *testing.T) {
|
||||
setenv(t, "KUBECONFIG", "testdata/kubeconfig.google.yaml"+string(os.PathListSeparator)+"testdata/kubeconfig.keycloak.yaml")
|
||||
defer unsetenv(t, "KUBECONFIG")
|
||||
|
||||
config, err := loadByDefaultRules("")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load the configs: %s", err)
|
||||
}
|
||||
if w := "google@hello.k8s.local"; w != config.CurrentContext {
|
||||
t.Errorf("CurrentContext wants %s but %s", w, config.CurrentContext)
|
||||
}
|
||||
if _, ok := config.Contexts["google@hello.k8s.local"]; !ok {
|
||||
t.Errorf("Contexts[google@hello.k8s.local] is missing")
|
||||
}
|
||||
if _, ok := config.Contexts["keycloak@hello.k8s.local"]; !ok {
|
||||
t.Errorf("Contexts[keycloak@hello.k8s.local] is missing")
|
||||
}
|
||||
if _, ok := config.AuthInfos["google"]; !ok {
|
||||
t.Errorf("AuthInfos[google] is missing")
|
||||
}
|
||||
if _, ok := config.AuthInfos["keycloak"]; !ok {
|
||||
t.Errorf("AuthInfos[keycloak] is missing")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("keycloak.yaml>google.yaml", func(t *testing.T) {
|
||||
setenv(t, "KUBECONFIG", "testdata/kubeconfig.keycloak.yaml"+string(os.PathListSeparator)+"testdata/kubeconfig.google.yaml")
|
||||
defer unsetenv(t, "KUBECONFIG")
|
||||
|
||||
config, err := loadByDefaultRules("")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not load the configs: %s", err)
|
||||
}
|
||||
if w := "keycloak@hello.k8s.local"; w != config.CurrentContext {
|
||||
t.Errorf("CurrentContext wants %s but %s", w, config.CurrentContext)
|
||||
}
|
||||
if _, ok := config.Contexts["google@hello.k8s.local"]; !ok {
|
||||
t.Errorf("Contexts[google@hello.k8s.local] is missing")
|
||||
}
|
||||
if _, ok := config.Contexts["keycloak@hello.k8s.local"]; !ok {
|
||||
t.Errorf("Contexts[keycloak@hello.k8s.local] is missing")
|
||||
}
|
||||
if _, ok := config.AuthInfos["google"]; !ok {
|
||||
t.Errorf("AuthInfos[google] is missing")
|
||||
}
|
||||
if _, ok := config.AuthInfos["keycloak"]; !ok {
|
||||
t.Errorf("AuthInfos[keycloak] is missing")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func setenv(t *testing.T, key, value string) {
|
||||
t.Helper()
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
t.Fatalf("Could not set the env var %s=%s: %s", key, value, err)
|
||||
}
|
||||
}
|
||||
|
||||
func unsetenv(t *testing.T, key string) {
|
||||
t.Helper()
|
||||
if err := os.Unsetenv(key); err != nil {
|
||||
t.Fatalf("Could not unset the env var %s: %s", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_findCurrentAuthProvider(t *testing.T) {
|
||||
t.Run("CurrentContext", func(t *testing.T) {
|
||||
auth, err := findCurrentAuthProvider(&api.Config{
|
||||
CurrentContext: "theContext",
|
||||
Contexts: map[string]*api.Context{
|
||||
"theContext": {
|
||||
AuthInfo: "theUser",
|
||||
},
|
||||
},
|
||||
AuthInfos: map[string]*api.AuthInfo{
|
||||
"theUser": {
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
AuthProvider: &api.AuthProviderConfig{
|
||||
Name: "oidc",
|
||||
Config: map[string]string{
|
||||
"idp-issuer-url": "https://accounts.google.com",
|
||||
"client-id": "GOOGLE_CLIENT_ID",
|
||||
"client-secret": "GOOGLE_CLIENT_SECRET",
|
||||
"idp-certificate-authority": "/path/to/cert",
|
||||
"idp-certificate-authority-data": "BASE64",
|
||||
"extra-scopes": "email,profile",
|
||||
"id-token": "YOUR_ID_TOKEN",
|
||||
"refresh-token": "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not find the current auth: %s", err)
|
||||
}
|
||||
want := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
ContextName: "theContext",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "GOOGLE_CLIENT_ID",
|
||||
ClientSecret: "GOOGLE_CLIENT_SECRET",
|
||||
IDPCertificateAuthority: "/path/to/cert",
|
||||
IDPCertificateAuthorityData: "BASE64",
|
||||
ExtraScopes: []string{"email", "profile"},
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
}
|
||||
if diff := deep.Equal(want, auth); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ByContextName", func(t *testing.T) {
|
||||
auth, err := findCurrentAuthProvider(&api.Config{
|
||||
Contexts: map[string]*api.Context{
|
||||
"theContext": {
|
||||
AuthInfo: "theUser",
|
||||
},
|
||||
},
|
||||
AuthInfos: map[string]*api.AuthInfo{
|
||||
"theUser": {
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
AuthProvider: &api.AuthProviderConfig{
|
||||
Name: "oidc",
|
||||
Config: map[string]string{
|
||||
"idp-issuer-url": "https://accounts.google.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, "theContext", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not find the current auth: %s", err)
|
||||
}
|
||||
want := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
ContextName: "theContext",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
},
|
||||
}
|
||||
if diff := deep.Equal(want, auth); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ByUserName", func(t *testing.T) {
|
||||
auth, err := findCurrentAuthProvider(&api.Config{
|
||||
AuthInfos: map[string]*api.AuthInfo{
|
||||
"theUser": {
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
AuthProvider: &api.AuthProviderConfig{
|
||||
Name: "oidc",
|
||||
Config: map[string]string{
|
||||
"idp-issuer-url": "https://accounts.google.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, "", "theUser")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not find the current auth: %s", err)
|
||||
}
|
||||
want := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
},
|
||||
}
|
||||
if diff := deep.Equal(want, auth); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoConfig", func(t *testing.T) {
|
||||
_, err := findCurrentAuthProvider(&api.Config{
|
||||
AuthInfos: map[string]*api.AuthInfo{
|
||||
"theUser": {
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
AuthProvider: &api.AuthProviderConfig{
|
||||
Name: "oidc",
|
||||
},
|
||||
},
|
||||
},
|
||||
}, "", "theUser")
|
||||
if err == nil {
|
||||
t.Fatalf("wants error but nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NotOIDC", func(t *testing.T) {
|
||||
_, err := findCurrentAuthProvider(&api.Config{
|
||||
AuthInfos: map[string]*api.AuthInfo{
|
||||
"theUser": {
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
AuthProvider: &api.AuthProviderConfig{
|
||||
Name: "some",
|
||||
Config: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, "", "theUser")
|
||||
if err == nil {
|
||||
t.Fatalf("wants error but nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
17
adaptors/kubeconfig/testdata/kubeconfig.google.yaml
vendored
Normal file
17
adaptors/kubeconfig/testdata/kubeconfig.google.yaml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: v1
|
||||
clusters: []
|
||||
contexts:
|
||||
- context:
|
||||
cluster: hello.k8s.local
|
||||
user: google
|
||||
name: google@hello.k8s.local
|
||||
current-context: google@hello.k8s.local
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: google
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
client-id: CLIENT_ID.apps.googleusercontent.com
|
||||
name: oidc
|
||||
16
adaptors/kubeconfig/testdata/kubeconfig.keycloak.yaml
vendored
Normal file
16
adaptors/kubeconfig/testdata/kubeconfig.keycloak.yaml
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
apiVersion: v1
|
||||
contexts:
|
||||
- context:
|
||||
cluster: hello.k8s.local
|
||||
user: keycloak
|
||||
name: keycloak@hello.k8s.local
|
||||
current-context: keycloak@hello.k8s.local
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: keycloak
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
client-id: kubernetes
|
||||
name: oidc
|
||||
51
adaptors/kubeconfig/write.go
Normal file
51
adaptors/kubeconfig/write.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"golang.org/x/xerrors"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
func (*Kubeconfig) UpdateAuthProvider(auth *kubeconfig.AuthProvider) error {
|
||||
config, err := clientcmd.LoadFromFile(auth.LocationOfOrigin)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("could not load %s: %w", auth.LocationOfOrigin, err)
|
||||
}
|
||||
userNode, ok := config.AuthInfos[string(auth.UserName)]
|
||||
if !ok {
|
||||
return xerrors.Errorf("user %s does not exist", auth.UserName)
|
||||
}
|
||||
if userNode.AuthProvider == nil {
|
||||
return xerrors.Errorf("auth-provider is missing")
|
||||
}
|
||||
if userNode.AuthProvider.Name != "oidc" {
|
||||
return xerrors.Errorf("auth-provider must be oidc but is %s", userNode.AuthProvider.Name)
|
||||
}
|
||||
copyOIDCConfig(auth.OIDCConfig, userNode.AuthProvider.Config)
|
||||
if err := clientcmd.WriteToFile(*config, auth.LocationOfOrigin); err != nil {
|
||||
return xerrors.Errorf("could not update %s: %w", auth.LocationOfOrigin, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyOIDCConfig(config kubeconfig.OIDCConfig, m map[string]string) {
|
||||
setOrDeleteKey(m, "idp-issuer-url", config.IDPIssuerURL)
|
||||
setOrDeleteKey(m, "client-id", config.ClientID)
|
||||
setOrDeleteKey(m, "client-secret", config.ClientSecret)
|
||||
setOrDeleteKey(m, "idp-certificate-authority", config.IDPCertificateAuthority)
|
||||
setOrDeleteKey(m, "idp-certificate-authority-data", config.IDPCertificateAuthorityData)
|
||||
extraScopes := strings.Join(config.ExtraScopes, ",")
|
||||
setOrDeleteKey(m, "extra-scopes", extraScopes)
|
||||
setOrDeleteKey(m, "id-token", config.IDToken)
|
||||
setOrDeleteKey(m, "refresh-token", config.RefreshToken)
|
||||
}
|
||||
|
||||
func setOrDeleteKey(m map[string]string, key, value string) {
|
||||
if value == "" {
|
||||
delete(m, key)
|
||||
return
|
||||
}
|
||||
m[key] = value
|
||||
}
|
||||
138
adaptors/kubeconfig/write_test.go
Normal file
138
adaptors/kubeconfig/write_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
)
|
||||
|
||||
func TestKubeconfig_UpdateAuth(t *testing.T) {
|
||||
var k Kubeconfig
|
||||
|
||||
t.Run("MinimumKeys", func(t *testing.T) {
|
||||
f := newKubeconfigFile(t)
|
||||
defer func() {
|
||||
if err := os.Remove(f.Name()); err != nil {
|
||||
t.Errorf("Could not remove the temp file: %s", err)
|
||||
}
|
||||
}()
|
||||
if err := k.UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: f.Name(),
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "GOOGLE_CLIENT_ID",
|
||||
ClientSecret: "GOOGLE_CLIENT_SECRET",
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Could not update auth: %s", err)
|
||||
}
|
||||
b, err := ioutil.ReadFile(f.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Could not read kubeconfig: %s", err)
|
||||
}
|
||||
|
||||
want := `apiVersion: v1
|
||||
clusters: []
|
||||
contexts: []
|
||||
current-context: ""
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: google
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
client-id: GOOGLE_CLIENT_ID
|
||||
client-secret: GOOGLE_CLIENT_SECRET
|
||||
id-token: YOUR_ID_TOKEN
|
||||
idp-issuer-url: https://accounts.google.com
|
||||
refresh-token: YOUR_REFRESH_TOKEN
|
||||
name: oidc
|
||||
`
|
||||
if want != string(b) {
|
||||
t.Errorf("---- kubeconfig wants ----\n%s\n---- but ----\n%s", want, string(b))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("FullKeys", func(t *testing.T) {
|
||||
f := newKubeconfigFile(t)
|
||||
defer func() {
|
||||
if err := os.Remove(f.Name()); err != nil {
|
||||
t.Errorf("Could not remove the temp file: %s", err)
|
||||
}
|
||||
}()
|
||||
if err := k.UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: f.Name(),
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "GOOGLE_CLIENT_ID",
|
||||
ClientSecret: "GOOGLE_CLIENT_SECRET",
|
||||
IDPCertificateAuthority: "/path/to/cert",
|
||||
IDPCertificateAuthorityData: "BASE64",
|
||||
ExtraScopes: []string{"email", "profile"},
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Could not update auth: %s", err)
|
||||
}
|
||||
b, err := ioutil.ReadFile(f.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Could not read kubeconfig: %s", err)
|
||||
}
|
||||
|
||||
want := `apiVersion: v1
|
||||
clusters: []
|
||||
contexts: []
|
||||
current-context: ""
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: google
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
client-id: GOOGLE_CLIENT_ID
|
||||
client-secret: GOOGLE_CLIENT_SECRET
|
||||
extra-scopes: email,profile
|
||||
id-token: YOUR_ID_TOKEN
|
||||
idp-certificate-authority: /path/to/cert
|
||||
idp-certificate-authority-data: BASE64
|
||||
idp-issuer-url: https://accounts.google.com
|
||||
refresh-token: YOUR_REFRESH_TOKEN
|
||||
name: oidc
|
||||
`
|
||||
if want != string(b) {
|
||||
t.Errorf("---- kubeconfig wants ----\n%s\n---- but ----\n%s", want, string(b))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func newKubeconfigFile(t *testing.T) *os.File {
|
||||
content := `apiVersion: v1
|
||||
clusters: []
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: google
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
idp-issuer-url: https://accounts.google.com
|
||||
name: oidc`
|
||||
f, err := ioutil.TempFile("", "kubeconfig")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create a file: %s", err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := f.Write([]byte(content)); err != nil {
|
||||
t.Fatalf("Could not write kubeconfig: %s", err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
56
adaptors/logger/logger.go
Normal file
56
adaptors/logger/logger.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
)
|
||||
|
||||
// Set provides an implementation and interface for Logger.
|
||||
var Set = wire.NewSet(
|
||||
New,
|
||||
)
|
||||
|
||||
// New returns a Logger with the standard log.Logger for messages and debug.
|
||||
func New() adaptors.Logger {
|
||||
return &Logger{
|
||||
stdLogger: log.New(os.Stderr, "", 0),
|
||||
debugLogger: log.New(os.Stderr, "", log.Ltime|log.Lmicroseconds|log.Lshortfile),
|
||||
}
|
||||
}
|
||||
|
||||
func NewWith(s stdLogger, d debugLogger) *Logger {
|
||||
return &Logger{s, d, 0}
|
||||
}
|
||||
|
||||
type stdLogger interface {
|
||||
Printf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
type debugLogger interface {
|
||||
Output(calldepth int, s string) error
|
||||
}
|
||||
|
||||
// Logger wraps the standard log.Logger and just provides debug level.
|
||||
type Logger struct {
|
||||
stdLogger
|
||||
debugLogger
|
||||
level adaptors.LogLevel
|
||||
}
|
||||
|
||||
func (l *Logger) Debugf(level adaptors.LogLevel, format string, v ...interface{}) {
|
||||
if l.IsEnabled(level) {
|
||||
_ = l.debugLogger.Output(2, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) SetLevel(level adaptors.LogLevel) {
|
||||
l.level = level
|
||||
}
|
||||
|
||||
func (l *Logger) IsEnabled(level adaptors.LogLevel) bool {
|
||||
return level <= l.level
|
||||
}
|
||||
62
adaptors/logger/logger_test.go
Normal file
62
adaptors/logger/logger_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
)
|
||||
|
||||
type mockDebugLogger struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (l *mockDebugLogger) Output(int, string) error {
|
||||
l.count++
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestLogger_Debugf(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
loggerLevel adaptors.LogLevel
|
||||
debugfLevel adaptors.LogLevel
|
||||
count int
|
||||
}{
|
||||
{0, 0, 1},
|
||||
{0, 1, 0},
|
||||
|
||||
{1, 0, 1},
|
||||
{1, 1, 1},
|
||||
{1, 2, 0},
|
||||
|
||||
{2, 1, 1},
|
||||
{2, 2, 1},
|
||||
{2, 3, 0},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) {
|
||||
m := &mockDebugLogger{}
|
||||
l := &Logger{debugLogger: m, level: c.loggerLevel}
|
||||
l.Debugf(c.debugfLevel, "hello")
|
||||
if m.count != c.count {
|
||||
t.Errorf("count wants %d but %d", c.count, m.count)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type mockStdLogger struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (l *mockStdLogger) Printf(format string, v ...interface{}) {
|
||||
l.count++
|
||||
}
|
||||
|
||||
func TestLogger_Printf(t *testing.T) {
|
||||
m := &mockStdLogger{}
|
||||
l := &Logger{stdLogger: m}
|
||||
l.Printf("hello")
|
||||
if m.count != 1 {
|
||||
t.Errorf("count wants %d but %d", 1, m.count)
|
||||
}
|
||||
}
|
||||
31
adaptors/mock_adaptors/logger.go
Normal file
31
adaptors/mock_adaptors/logger.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package mock_adaptors
|
||||
|
||||
import (
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
)
|
||||
|
||||
func NewLogger(t testingLogger, ctrl *gomock.Controller) *Logger {
|
||||
return &Logger{
|
||||
MockLogger: NewMockLogger(ctrl),
|
||||
testingLogger: t,
|
||||
}
|
||||
}
|
||||
|
||||
type testingLogger interface {
|
||||
Logf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
// Logger provides mock feature but overrides output methods with actual logging.
|
||||
type Logger struct {
|
||||
*MockLogger
|
||||
testingLogger testingLogger
|
||||
}
|
||||
|
||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||
l.testingLogger.Logf(format, v...)
|
||||
}
|
||||
|
||||
func (l *Logger) Debugf(level adaptors.LogLevel, format string, v ...interface{}) {
|
||||
l.testingLogger.Logf(format, v...)
|
||||
}
|
||||
296
adaptors/mock_adaptors/mock_adaptors.go
Normal file
296
adaptors/mock_adaptors/mock_adaptors.go
Normal file
@@ -0,0 +1,296 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/int128/kubelogin/adaptors (interfaces: Kubeconfig,OIDC,OIDCClient,Env,Logger)
|
||||
|
||||
// Package mock_adaptors is a generated GoMock package.
|
||||
package mock_adaptors
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
adaptors "github.com/int128/kubelogin/adaptors"
|
||||
kubeconfig "github.com/int128/kubelogin/models/kubeconfig"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockKubeconfig is a mock of Kubeconfig interface
|
||||
type MockKubeconfig struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockKubeconfigMockRecorder
|
||||
}
|
||||
|
||||
// MockKubeconfigMockRecorder is the mock recorder for MockKubeconfig
|
||||
type MockKubeconfigMockRecorder struct {
|
||||
mock *MockKubeconfig
|
||||
}
|
||||
|
||||
// NewMockKubeconfig creates a new mock instance
|
||||
func NewMockKubeconfig(ctrl *gomock.Controller) *MockKubeconfig {
|
||||
mock := &MockKubeconfig{ctrl: ctrl}
|
||||
mock.recorder = &MockKubeconfigMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockKubeconfig) EXPECT() *MockKubeconfigMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// GetCurrentAuthProvider mocks base method
|
||||
func (m *MockKubeconfig) GetCurrentAuthProvider(arg0 string, arg1 kubeconfig.ContextName, arg2 kubeconfig.UserName) (*kubeconfig.AuthProvider, error) {
|
||||
ret := m.ctrl.Call(m, "GetCurrentAuthProvider", arg0, arg1, arg2)
|
||||
ret0, _ := ret[0].(*kubeconfig.AuthProvider)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetCurrentAuthProvider indicates an expected call of GetCurrentAuthProvider
|
||||
func (mr *MockKubeconfigMockRecorder) GetCurrentAuthProvider(arg0, arg1, arg2 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrentAuthProvider", reflect.TypeOf((*MockKubeconfig)(nil).GetCurrentAuthProvider), arg0, arg1, arg2)
|
||||
}
|
||||
|
||||
// UpdateAuthProvider mocks base method
|
||||
func (m *MockKubeconfig) UpdateAuthProvider(arg0 *kubeconfig.AuthProvider) error {
|
||||
ret := m.ctrl.Call(m, "UpdateAuthProvider", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// UpdateAuthProvider indicates an expected call of UpdateAuthProvider
|
||||
func (mr *MockKubeconfigMockRecorder) UpdateAuthProvider(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAuthProvider", reflect.TypeOf((*MockKubeconfig)(nil).UpdateAuthProvider), arg0)
|
||||
}
|
||||
|
||||
// MockOIDC is a mock of OIDC interface
|
||||
type MockOIDC struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockOIDCMockRecorder
|
||||
}
|
||||
|
||||
// MockOIDCMockRecorder is the mock recorder for MockOIDC
|
||||
type MockOIDCMockRecorder struct {
|
||||
mock *MockOIDC
|
||||
}
|
||||
|
||||
// NewMockOIDC creates a new mock instance
|
||||
func NewMockOIDC(ctrl *gomock.Controller) *MockOIDC {
|
||||
mock := &MockOIDC{ctrl: ctrl}
|
||||
mock.recorder = &MockOIDCMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockOIDC) EXPECT() *MockOIDCMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// New mocks base method
|
||||
func (m *MockOIDC) New(arg0 context.Context, arg1 adaptors.OIDCClientConfig) (adaptors.OIDCClient, error) {
|
||||
ret := m.ctrl.Call(m, "New", arg0, arg1)
|
||||
ret0, _ := ret[0].(adaptors.OIDCClient)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// New indicates an expected call of New
|
||||
func (mr *MockOIDCMockRecorder) New(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockOIDC)(nil).New), arg0, arg1)
|
||||
}
|
||||
|
||||
// MockOIDCClient is a mock of OIDCClient interface
|
||||
type MockOIDCClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockOIDCClientMockRecorder
|
||||
}
|
||||
|
||||
// MockOIDCClientMockRecorder is the mock recorder for MockOIDCClient
|
||||
type MockOIDCClientMockRecorder struct {
|
||||
mock *MockOIDCClient
|
||||
}
|
||||
|
||||
// NewMockOIDCClient creates a new mock instance
|
||||
func NewMockOIDCClient(ctrl *gomock.Controller) *MockOIDCClient {
|
||||
mock := &MockOIDCClient{ctrl: ctrl}
|
||||
mock.recorder = &MockOIDCClientMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockOIDCClient) EXPECT() *MockOIDCClientMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// AuthenticateByCode mocks base method
|
||||
func (m *MockOIDCClient) AuthenticateByCode(arg0 context.Context, arg1 adaptors.OIDCAuthenticateByCodeIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
ret := m.ctrl.Call(m, "AuthenticateByCode", arg0, arg1)
|
||||
ret0, _ := ret[0].(*adaptors.OIDCAuthenticateOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// AuthenticateByCode indicates an expected call of AuthenticateByCode
|
||||
func (mr *MockOIDCClientMockRecorder) AuthenticateByCode(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AuthenticateByCode", reflect.TypeOf((*MockOIDCClient)(nil).AuthenticateByCode), arg0, arg1)
|
||||
}
|
||||
|
||||
// AuthenticateByPassword mocks base method
|
||||
func (m *MockOIDCClient) AuthenticateByPassword(arg0 context.Context, arg1 adaptors.OIDCAuthenticateByPasswordIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
ret := m.ctrl.Call(m, "AuthenticateByPassword", arg0, arg1)
|
||||
ret0, _ := ret[0].(*adaptors.OIDCAuthenticateOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// AuthenticateByPassword indicates an expected call of AuthenticateByPassword
|
||||
func (mr *MockOIDCClientMockRecorder) AuthenticateByPassword(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AuthenticateByPassword", reflect.TypeOf((*MockOIDCClient)(nil).AuthenticateByPassword), arg0, arg1)
|
||||
}
|
||||
|
||||
// Refresh mocks base method
|
||||
func (m *MockOIDCClient) Refresh(arg0 context.Context, arg1 adaptors.OIDCRefreshIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
ret := m.ctrl.Call(m, "Refresh", arg0, arg1)
|
||||
ret0, _ := ret[0].(*adaptors.OIDCAuthenticateOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Refresh indicates an expected call of Refresh
|
||||
func (mr *MockOIDCClientMockRecorder) Refresh(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Refresh", reflect.TypeOf((*MockOIDCClient)(nil).Refresh), arg0, arg1)
|
||||
}
|
||||
|
||||
// Verify mocks base method
|
||||
func (m *MockOIDCClient) Verify(arg0 context.Context, arg1 adaptors.OIDCVerifyIn) (*adaptors.OIDCVerifyOut, error) {
|
||||
ret := m.ctrl.Call(m, "Verify", arg0, arg1)
|
||||
ret0, _ := ret[0].(*adaptors.OIDCVerifyOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Verify indicates an expected call of Verify
|
||||
func (mr *MockOIDCClientMockRecorder) Verify(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Verify", reflect.TypeOf((*MockOIDCClient)(nil).Verify), arg0, arg1)
|
||||
}
|
||||
|
||||
// MockEnv is a mock of Env interface
|
||||
type MockEnv struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockEnvMockRecorder
|
||||
}
|
||||
|
||||
// MockEnvMockRecorder is the mock recorder for MockEnv
|
||||
type MockEnvMockRecorder struct {
|
||||
mock *MockEnv
|
||||
}
|
||||
|
||||
// NewMockEnv creates a new mock instance
|
||||
func NewMockEnv(ctrl *gomock.Controller) *MockEnv {
|
||||
mock := &MockEnv{ctrl: ctrl}
|
||||
mock.recorder = &MockEnvMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockEnv) EXPECT() *MockEnvMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Exec mocks base method
|
||||
func (m *MockEnv) Exec(arg0 context.Context, arg1 string, arg2 []string) (int, error) {
|
||||
ret := m.ctrl.Call(m, "Exec", arg0, arg1, arg2)
|
||||
ret0, _ := ret[0].(int)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Exec indicates an expected call of Exec
|
||||
func (mr *MockEnvMockRecorder) Exec(arg0, arg1, arg2 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Exec", reflect.TypeOf((*MockEnv)(nil).Exec), arg0, arg1, arg2)
|
||||
}
|
||||
|
||||
// ReadPassword mocks base method
|
||||
func (m *MockEnv) ReadPassword(arg0 string) (string, error) {
|
||||
ret := m.ctrl.Call(m, "ReadPassword", arg0)
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ReadPassword indicates an expected call of ReadPassword
|
||||
func (mr *MockEnvMockRecorder) ReadPassword(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadPassword", reflect.TypeOf((*MockEnv)(nil).ReadPassword), arg0)
|
||||
}
|
||||
|
||||
// MockLogger is a mock of Logger interface
|
||||
type MockLogger struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockLoggerMockRecorder
|
||||
}
|
||||
|
||||
// MockLoggerMockRecorder is the mock recorder for MockLogger
|
||||
type MockLoggerMockRecorder struct {
|
||||
mock *MockLogger
|
||||
}
|
||||
|
||||
// NewMockLogger creates a new mock instance
|
||||
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
|
||||
mock := &MockLogger{ctrl: ctrl}
|
||||
mock.recorder = &MockLoggerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Debugf mocks base method
|
||||
func (m *MockLogger) Debugf(arg0 adaptors.LogLevel, arg1 string, arg2 ...interface{}) {
|
||||
varargs := []interface{}{arg0, arg1}
|
||||
for _, a := range arg2 {
|
||||
varargs = append(varargs, a)
|
||||
}
|
||||
m.ctrl.Call(m, "Debugf", varargs...)
|
||||
}
|
||||
|
||||
// Debugf indicates an expected call of Debugf
|
||||
func (mr *MockLoggerMockRecorder) Debugf(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
varargs := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debugf", reflect.TypeOf((*MockLogger)(nil).Debugf), varargs...)
|
||||
}
|
||||
|
||||
// IsEnabled mocks base method
|
||||
func (m *MockLogger) IsEnabled(arg0 adaptors.LogLevel) bool {
|
||||
ret := m.ctrl.Call(m, "IsEnabled", arg0)
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// IsEnabled indicates an expected call of IsEnabled
|
||||
func (mr *MockLoggerMockRecorder) IsEnabled(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsEnabled", reflect.TypeOf((*MockLogger)(nil).IsEnabled), arg0)
|
||||
}
|
||||
|
||||
// Printf mocks base method
|
||||
func (m *MockLogger) Printf(arg0 string, arg1 ...interface{}) {
|
||||
varargs := []interface{}{arg0}
|
||||
for _, a := range arg1 {
|
||||
varargs = append(varargs, a)
|
||||
}
|
||||
m.ctrl.Call(m, "Printf", varargs...)
|
||||
}
|
||||
|
||||
// Printf indicates an expected call of Printf
|
||||
func (mr *MockLoggerMockRecorder) Printf(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||
varargs := append([]interface{}{arg0}, arg1...)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Printf", reflect.TypeOf((*MockLogger)(nil).Printf), varargs...)
|
||||
}
|
||||
|
||||
// SetLevel mocks base method
|
||||
func (m *MockLogger) SetLevel(arg0 adaptors.LogLevel) {
|
||||
m.ctrl.Call(m, "SetLevel", arg0)
|
||||
}
|
||||
|
||||
// SetLevel indicates an expected call of SetLevel
|
||||
func (mr *MockLoggerMockRecorder) SetLevel(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLevel", reflect.TypeOf((*MockLogger)(nil).SetLevel), arg0)
|
||||
}
|
||||
50
adaptors/oidc/logging/transport.go
Normal file
50
adaptors/oidc/logging/transport.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
)
|
||||
|
||||
const (
|
||||
logLevelDumpHeaders = 2
|
||||
logLevelDumpBody = 3
|
||||
)
|
||||
|
||||
type Transport struct {
|
||||
Base http.RoundTripper
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if !t.IsDumpEnabled() {
|
||||
return t.Base.RoundTrip(req)
|
||||
}
|
||||
|
||||
reqDump, err := httputil.DumpRequestOut(req, t.IsDumpBodyEnabled())
|
||||
if err != nil {
|
||||
t.Logger.Debugf(logLevelDumpHeaders, "Error: could not dump the request: %s", err)
|
||||
return t.Base.RoundTrip(req)
|
||||
}
|
||||
t.Logger.Debugf(logLevelDumpHeaders, "%s", string(reqDump))
|
||||
resp, err := t.Base.RoundTrip(req)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
respDump, err := httputil.DumpResponse(resp, t.IsDumpBodyEnabled())
|
||||
if err != nil {
|
||||
t.Logger.Debugf(logLevelDumpHeaders, "Error: could not dump the response: %s", err)
|
||||
return resp, err
|
||||
}
|
||||
t.Logger.Debugf(logLevelDumpHeaders, "%s", string(respDump))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (t *Transport) IsDumpEnabled() bool {
|
||||
return t.Logger.IsEnabled(logLevelDumpHeaders)
|
||||
}
|
||||
|
||||
func (t *Transport) IsDumpBodyEnabled() bool {
|
||||
return t.Logger.IsEnabled(logLevelDumpBody)
|
||||
}
|
||||
90
adaptors/oidc/logging/transport_test.go
Normal file
90
adaptors/oidc/logging/transport_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/mock_adaptors"
|
||||
)
|
||||
|
||||
type mockTransport struct {
|
||||
req *http.Request
|
||||
resp *http.Response
|
||||
}
|
||||
|
||||
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
t.req = req
|
||||
return t.resp, nil
|
||||
}
|
||||
|
||||
func TestLoggingTransport_RoundTrip(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().
|
||||
IsEnabled(gomock.Any()).
|
||||
Return(true).
|
||||
AnyTimes()
|
||||
|
||||
req := httptest.NewRequest("GET", "http://example.com/hello", nil)
|
||||
resp, err := http.ReadResponse(bufio.NewReader(strings.NewReader(`HTTP/1.1 200 OK
|
||||
Host: example.com
|
||||
|
||||
dummy`)), req)
|
||||
if err != nil {
|
||||
t.Errorf("could not create a response: %s", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
transport := &Transport{
|
||||
Base: &mockTransport{resp: resp},
|
||||
Logger: logger,
|
||||
}
|
||||
gotResp, err := transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Errorf("RoundTrip error: %s", err)
|
||||
}
|
||||
if gotResp != resp {
|
||||
t.Errorf("resp wants %v but %v", resp, gotResp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggingTransport_IsDumpEnabled(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().
|
||||
IsEnabled(adaptors.LogLevel(logLevelDumpHeaders)).
|
||||
Return(true)
|
||||
|
||||
transport := &Transport{
|
||||
Logger: logger,
|
||||
}
|
||||
if transport.IsDumpEnabled() != true {
|
||||
t.Errorf("IsDumpEnabled wants true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggingTransport_IsDumpBodyEnabled(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
logger := mock_adaptors.NewLogger(t, ctrl)
|
||||
logger.EXPECT().
|
||||
IsEnabled(adaptors.LogLevel(logLevelDumpBody)).
|
||||
Return(true)
|
||||
|
||||
transport := &Transport{
|
||||
Logger: logger,
|
||||
}
|
||||
if transport.IsDumpBodyEnabled() != true {
|
||||
t.Errorf("IsDumpBodyEnabled wants true")
|
||||
}
|
||||
}
|
||||
228
adaptors/oidc/oidc.go
Normal file
228
adaptors/oidc/oidc.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package oidc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-oidc"
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/oidc/logging"
|
||||
"github.com/int128/kubelogin/adaptors/oidc/tls"
|
||||
"github.com/int128/oauth2cli"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Set provides an implementation and interface for OIDC.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Factory), "*"),
|
||||
wire.Bind(new(adaptors.OIDC), new(*Factory)),
|
||||
)
|
||||
|
||||
type Factory struct {
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
// New returns an instance of adaptors.OIDCClient with the given configuration.
|
||||
func (f *Factory) New(ctx context.Context, config adaptors.OIDCClientConfig) (adaptors.OIDCClient, error) {
|
||||
tlsConfig, err := tls.NewConfig(config, f.Logger)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not initialize TLS config: %w", err)
|
||||
}
|
||||
baseTransport := &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
}
|
||||
loggingTransport := &logging.Transport{
|
||||
Base: baseTransport,
|
||||
Logger: f.Logger,
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: loggingTransport,
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
||||
provider, err := oidc.NewProvider(ctx, config.Config.IDPIssuerURL)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not discovery the OIDC issuer: %w", err)
|
||||
}
|
||||
return &client{
|
||||
httpClient: httpClient,
|
||||
provider: provider,
|
||||
oauth2Config: oauth2.Config{
|
||||
Endpoint: provider.Endpoint(),
|
||||
ClientID: config.Config.ClientID,
|
||||
ClientSecret: config.Config.ClientSecret,
|
||||
Scopes: append(config.Config.ExtraScopes, oidc.ScopeOpenID),
|
||||
},
|
||||
logger: f.Logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type client struct {
|
||||
httpClient *http.Client
|
||||
provider *oidc.Provider
|
||||
oauth2Config oauth2.Config
|
||||
logger adaptors.Logger
|
||||
}
|
||||
|
||||
// AuthenticateByCode performs the authorization code flow.
|
||||
func (c *client) AuthenticateByCode(ctx context.Context, in adaptors.OIDCAuthenticateByCodeIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
if c.httpClient != nil {
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)
|
||||
}
|
||||
nonce, err := newNonce()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not generate a nonce parameter")
|
||||
}
|
||||
config := oauth2cli.Config{
|
||||
OAuth2Config: c.oauth2Config,
|
||||
LocalServerPort: in.LocalServerPort,
|
||||
SkipOpenBrowser: in.SkipOpenBrowser,
|
||||
AuthCodeOptions: []oauth2.AuthCodeOption{oauth2.AccessTypeOffline, oidc.Nonce(nonce)},
|
||||
ShowLocalServerURL: in.ShowLocalServerURL.ShowLocalServerURL,
|
||||
}
|
||||
token, err := oauth2cli.GetToken(ctx, config)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not get a token: %w", err)
|
||||
}
|
||||
idToken, ok := token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("id_token is missing in the token response: %s", token)
|
||||
}
|
||||
verifier := c.provider.Verifier(&oidc.Config{ClientID: c.oauth2Config.ClientID})
|
||||
verifiedIDToken, err := verifier.Verify(ctx, idToken)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not verify the id_token: %w", err)
|
||||
}
|
||||
if verifiedIDToken.Nonce != nonce {
|
||||
return nil, xerrors.Errorf("nonce of ID token did not match (want %s but was %s)", nonce, verifiedIDToken.Nonce)
|
||||
}
|
||||
claims, err := dumpClaims(verifiedIDToken)
|
||||
if err != nil {
|
||||
c.logger.Debugf(1, "incomplete claims of the ID token: %w", err)
|
||||
}
|
||||
return &adaptors.OIDCAuthenticateOut{
|
||||
IDToken: idToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
IDTokenExpiry: verifiedIDToken.Expiry,
|
||||
IDTokenClaims: claims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newNonce() (string, error) {
|
||||
var n uint64
|
||||
if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil {
|
||||
return "", xerrors.Errorf("error while reading random: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("%x", n), nil
|
||||
}
|
||||
|
||||
// AuthenticateByPassword performs the resource owner password credentials flow.
|
||||
func (c *client) AuthenticateByPassword(ctx context.Context, in adaptors.OIDCAuthenticateByPasswordIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
if c.httpClient != nil {
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)
|
||||
}
|
||||
token, err := c.oauth2Config.PasswordCredentialsToken(ctx, in.Username, in.Password)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not get a token: %w", err)
|
||||
}
|
||||
idToken, ok := token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("id_token is missing in the token response: %s", token)
|
||||
}
|
||||
verifier := c.provider.Verifier(&oidc.Config{ClientID: c.oauth2Config.ClientID})
|
||||
verifiedIDToken, err := verifier.Verify(ctx, idToken)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not verify the id_token: %w", err)
|
||||
}
|
||||
claims, err := dumpClaims(verifiedIDToken)
|
||||
if err != nil {
|
||||
c.logger.Debugf(1, "incomplete claims of the ID token: %w", err)
|
||||
}
|
||||
return &adaptors.OIDCAuthenticateOut{
|
||||
IDToken: idToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
IDTokenExpiry: verifiedIDToken.Expiry,
|
||||
IDTokenClaims: claims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Verify checks client ID and signature of the ID token.
|
||||
// This does not check the expiration and caller should check it.
|
||||
func (c *client) Verify(ctx context.Context, in adaptors.OIDCVerifyIn) (*adaptors.OIDCVerifyOut, error) {
|
||||
if c.httpClient != nil {
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)
|
||||
}
|
||||
verifier := c.provider.Verifier(&oidc.Config{
|
||||
ClientID: c.oauth2Config.ClientID,
|
||||
SkipExpiryCheck: true,
|
||||
})
|
||||
verifiedIDToken, err := verifier.Verify(ctx, in.IDToken)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not verify the id_token: %w", err)
|
||||
}
|
||||
claims, err := dumpClaims(verifiedIDToken)
|
||||
if err != nil {
|
||||
c.logger.Debugf(1, "incomplete claims of the ID token: %w", err)
|
||||
}
|
||||
return &adaptors.OIDCVerifyOut{
|
||||
IDTokenExpiry: verifiedIDToken.Expiry,
|
||||
IDTokenClaims: claims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Refresh sends a refresh token request and returns a token set.
|
||||
func (c *client) Refresh(ctx context.Context, in adaptors.OIDCRefreshIn) (*adaptors.OIDCAuthenticateOut, error) {
|
||||
currentToken := &oauth2.Token{
|
||||
Expiry: time.Now(),
|
||||
RefreshToken: in.RefreshToken,
|
||||
}
|
||||
source := c.oauth2Config.TokenSource(ctx, currentToken)
|
||||
token, err := source.Token()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not refresh the token: %w", err)
|
||||
}
|
||||
idToken, ok := token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("id_token is missing in the token response: %s", token)
|
||||
}
|
||||
verifier := c.provider.Verifier(&oidc.Config{ClientID: c.oauth2Config.ClientID})
|
||||
verifiedIDToken, err := verifier.Verify(ctx, idToken)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not verify the id_token: %w", err)
|
||||
}
|
||||
claims, err := dumpClaims(verifiedIDToken)
|
||||
if err != nil {
|
||||
c.logger.Debugf(1, "incomplete claims of the ID token: %w", err)
|
||||
}
|
||||
return &adaptors.OIDCAuthenticateOut{
|
||||
IDToken: idToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
IDTokenExpiry: verifiedIDToken.Expiry,
|
||||
IDTokenClaims: claims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func dumpClaims(token *oidc.IDToken) (map[string]string, error) {
|
||||
var rawClaims map[string]interface{}
|
||||
err := token.Claims(&rawClaims)
|
||||
claims := make(map[string]string)
|
||||
for k, v := range rawClaims {
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
claims[k] = fmt.Sprintf("%f", v.(float64))
|
||||
default:
|
||||
claims[k] = fmt.Sprintf("%s", v)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return claims, xerrors.Errorf("error while decoding the ID token: %w", err)
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
29
adaptors/oidc/tls/testdata/Makefile
vendored
Normal file
29
adaptors/oidc/tls/testdata/Makefile
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
.PHONY: clean
|
||||
|
||||
all: ca1.crt ca1.crt.base64 ca2.crt ca2.crt.base64 ca3.crt ca3.crt.base64
|
||||
|
||||
clean:
|
||||
rm -v *.key *.csr *.crt *.base64
|
||||
|
||||
%.key:
|
||||
openssl genrsa -out $@ 1024
|
||||
|
||||
%.csr: %.key
|
||||
openssl req \
|
||||
-new \
|
||||
-key $< \
|
||||
-subj "/CN=Hello" \
|
||||
-days 3650 \
|
||||
-out $@
|
||||
openssl req -noout -text -in $@
|
||||
|
||||
%.crt: %.csr %.key
|
||||
openssl x509 -req \
|
||||
-signkey $*.key \
|
||||
-in $*.csr \
|
||||
-days 3650 \
|
||||
-out $@
|
||||
openssl x509 -text -in $@
|
||||
|
||||
%.crt.base64: %.crt
|
||||
base64 -i $< -o $@
|
||||
11
adaptors/oidc/tls/testdata/ca1.crt
vendored
Normal file
11
adaptors/oidc/tls/testdata/ca1.crt
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIBlzCCAQACCQCDf7Inwu3vkzANBgkqhkiG9w0BAQUFADAQMQ4wDAYDVQQDDAVI
|
||||
ZWxsbzAeFw0xOTA2MjQxMDQ0NDhaFw0yOTA2MjExMDQ0NDhaMBAxDjAMBgNVBAMM
|
||||
BUhlbGxvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCitZv5Go16nuHDRa2u
|
||||
nT5m1Q9tkr668pnhcP0TkyjD+oEB0lUz2SJEZEvOd1XVRRrPMSXrtybo9p0TqSGp
|
||||
Ig1gORWis/j/IR1sYdFutLKhtp6k1HvUiNosdO/K8K/AbO4QPWTGBAcqg//QkMKd
|
||||
ccgLY2PYczK/t8+6C7JYEHe5AwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBACkPsyme
|
||||
JFlj75fO54NH5WXZxBtoY7kV3yd5oO88BngE8ittaHuauQkkw/sC5x733SsJlPlF
|
||||
trah4CDMjq5d/okIbIJFKe7NGLi82f9zJ+o1fjDp97UvZHC0zhUx+RiEu3iZRfYM
|
||||
31Ht7QG63V5ScV3Zmi1nzfQc4jn8d40kXXcn
|
||||
-----END CERTIFICATE-----
|
||||
1
adaptors/oidc/tls/testdata/ca1.crt.base64
vendored
Normal file
1
adaptors/oidc/tls/testdata/ca1.crt.base64
vendored
Normal file
@@ -0,0 +1 @@
|
||||
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJsekNDQVFBQ0NRQ0RmN0lud3Uzdmt6QU5CZ2txaGtpRzl3MEJBUVVGQURBUU1RNHdEQVlEVlFRRERBVkkKWld4c2J6QWVGdzB4T1RBMk1qUXhNRFEwTkRoYUZ3MHlPVEEyTWpFeE1EUTBORGhhTUJBeERqQU1CZ05WQkFNTQpCVWhsYkd4dk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRQ2l0WnY1R28xNm51SERSYTJ1Cm5UNW0xUTl0a3I2NjhwbmhjUDBUa3lqRCtvRUIwbFV6MlNKRVpFdk9kMVhWUlJyUE1TWHJ0eWJvOXAwVHFTR3AKSWcxZ09SV2lzL2ovSVIxc1lkRnV0TEtodHA2azFIdlVpTm9zZE8vSzhLL0FiTzRRUFdUR0JBY3FnLy9Ra01LZApjY2dMWTJQWWN6Sy90OCs2QzdKWUVIZTVBd0lEQVFBQk1BMEdDU3FHU0liM0RRRUJCUVVBQTRHQkFDa1BzeW1lCkpGbGo3NWZPNTROSDVXWFp4QnRvWTdrVjN5ZDVvTzg4Qm5nRThpdHRhSHVhdVFra3cvc0M1eDczM1NzSmxQbEYKdHJhaDRDRE1qcTVkL29rSWJJSkZLZTdOR0xpODJmOXpKK28xZmpEcDk3VXZaSEMwemhVeCtSaUV1M2laUmZZTQozMUh0N1FHNjNWNVNjVjNabWkxbnpmUWM0am44ZDQwa1hYY24KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
|
||||
11
adaptors/oidc/tls/testdata/ca2.crt
vendored
Normal file
11
adaptors/oidc/tls/testdata/ca2.crt
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIBlzCCAQACCQCuudlGZuJvODANBgkqhkiG9w0BAQUFADAQMQ4wDAYDVQQDDAVI
|
||||
ZWxsbzAeFw0xOTA2MjQxMDQ0NDhaFw0yOTA2MjExMDQ0NDhaMBAxDjAMBgNVBAMM
|
||||
BUhlbGxvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHnrC3q5aCXhULGTTg
|
||||
w7psUbrH3gpHEExxlw6Zj+UBZHFhxOccGYfHPvqKwfRAKfqkP6VzLdYsfF0fuMOX
|
||||
ZzFk2hB1eAdl2dsFIn4hMll+jDdo9x+7NKvAXgsFF174ZMVTW26aAME8s4OrNuZT
|
||||
Fdrp7byuUkwUbSzDC/B/ct9MFQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAGXsF0IA
|
||||
yP3g1UTLpld2P38dvMXLGN6gwn0S0oh7AQMckJ35yh8CN/2rAkBVujyvGILLhh2/
|
||||
teoIjM2BcZsrsKZ+Jkr177fRIunsd7a+v18M/3/pVvxPZdnztXspycxIacd7yVbG
|
||||
5wjN+X7rkoBLhd+BT9+W9O/i+Cu7K89JOO64
|
||||
-----END CERTIFICATE-----
|
||||
1
adaptors/oidc/tls/testdata/ca2.crt.base64
vendored
Normal file
1
adaptors/oidc/tls/testdata/ca2.crt.base64
vendored
Normal file
@@ -0,0 +1 @@
|
||||
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJsekNDQVFBQ0NRQ3V1ZGxHWnVKdk9EQU5CZ2txaGtpRzl3MEJBUVVGQURBUU1RNHdEQVlEVlFRRERBVkkKWld4c2J6QWVGdzB4T1RBMk1qUXhNRFEwTkRoYUZ3MHlPVEEyTWpFeE1EUTBORGhhTUJBeERqQU1CZ05WQkFNTQpCVWhsYkd4dk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRREhuckMzcTVhQ1hoVUxHVFRnCnc3cHNVYnJIM2dwSEVFeHhsdzZaaitVQlpIRmh4T2NjR1lmSFB2cUt3ZlJBS2Zxa1A2VnpMZFlzZkYwZnVNT1gKWnpGazJoQjFlQWRsMmRzRkluNGhNbGwrakRkbzl4KzdOS3ZBWGdzRkYxNzRaTVZUVzI2YUFNRThzNE9yTnVaVApGZHJwN2J5dVVrd1ViU3pEQy9CL2N0OU1GUUlEQVFBQk1BMEdDU3FHU0liM0RRRUJCUVVBQTRHQkFHWHNGMElBCnlQM2cxVVRMcGxkMlAzOGR2TVhMR042Z3duMFMwb2g3QVFNY2tKMzV5aDhDTi8yckFrQlZ1anl2R0lMTGhoMi8KdGVvSWpNMkJjWnNyc0taK0prcjE3N2ZSSXVuc2Q3YSt2MThNLzMvcFZ2eFBaZG56dFhzcHljeElhY2Q3eVZiRwo1d2pOK1g3cmtvQkxoZCtCVDkrVzlPL2krQ3U3Szg5Sk9PNjQKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
|
||||
11
adaptors/oidc/tls/testdata/ca3.crt
vendored
Normal file
11
adaptors/oidc/tls/testdata/ca3.crt
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIBlzCCAQACCQDyFQsG5rDcJTANBgkqhkiG9w0BAQUFADAQMQ4wDAYDVQQDDAVI
|
||||
ZWxsbzAeFw0xOTA2MjQxMDQ0NDhaFw0yOTA2MjExMDQ0NDhaMBAxDjAMBgNVBAMM
|
||||
BUhlbGxvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUBsXHnAjPL3ejyNRs
|
||||
INI0cp4Sv4HzgXmL6nypzTdEOT3UcqfvZYj3dr4FWZytxb6XgvyvIzoV++GS22cf
|
||||
arXwwv0Z6CWiJXI+WQFdsQRQoAt4ucIa046b18p6mCiHfaH98aCOq9K3sxTfNOm3
|
||||
kWAi6oFzB5C+6HalQ+rWFSVWHQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBALQfsBMR
|
||||
pxD9vzTmhw+rc0HKei9QMViIC3KYPdzvCCe0lMjrWzvcmTtUyCNJm2J2GwBVfyok
|
||||
zeUskYjinppBy/ZmzpWTeqTLOoeozgAh/Jgya5cPh01BP+pPFYmcQ5wOZHK5PPSP
|
||||
jvfqMeYs8TjXJRjdBKcMuZAN/8g2Ubtn+QbM
|
||||
-----END CERTIFICATE-----
|
||||
1
adaptors/oidc/tls/testdata/ca3.crt.base64
vendored
Normal file
1
adaptors/oidc/tls/testdata/ca3.crt.base64
vendored
Normal file
@@ -0,0 +1 @@
|
||||
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJsekNDQVFBQ0NRRHlGUXNHNXJEY0pUQU5CZ2txaGtpRzl3MEJBUVVGQURBUU1RNHdEQVlEVlFRRERBVkkKWld4c2J6QWVGdzB4T1RBMk1qUXhNRFEwTkRoYUZ3MHlPVEEyTWpFeE1EUTBORGhhTUJBeERqQU1CZ05WQkFNTQpCVWhsYkd4dk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRRFVCc1hIbkFqUEwzZWp5TlJzCklOSTBjcDRTdjRIemdYbUw2bnlwelRkRU9UM1VjcWZ2WllqM2RyNEZXWnl0eGI2WGd2eXZJem9WKytHUzIyY2YKYXJYd3d2MFo2Q1dpSlhJK1dRRmRzUVJRb0F0NHVjSWEwNDZiMThwNm1DaUhmYUg5OGFDT3E5SzNzeFRmTk9tMwprV0FpNm9GekI1Qys2SGFsUStyV0ZTVldIUUlEQVFBQk1BMEdDU3FHU0liM0RRRUJCUVVBQTRHQkFMUWZzQk1SCnB4RDl2elRtaHcrcmMwSEtlaTlRTVZpSUMzS1lQZHp2Q0NlMGxNanJXenZjbVR0VXlDTkptMkoyR3dCVmZ5b2sKemVVc2tZamlucHBCeS9abXpwV1RlcVRMT29lb3pnQWgvSmd5YTVjUGgwMUJQK3BQRlltY1E1d09aSEs1UFBTUApqdmZxTWVZczhUalhKUmpkQktjTXVaQU4vOGcyVWJ0bitRYk0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
|
||||
66
adaptors/oidc/tls/tls.go
Normal file
66
adaptors/oidc/tls/tls.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package tls
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// NewConfig returns a tls.Config with the given certificates and options.
|
||||
func NewConfig(config adaptors.OIDCClientConfig, logger adaptors.Logger) (*tls.Config, error) {
|
||||
pool := x509.NewCertPool()
|
||||
if config.Config.IDPCertificateAuthority != "" {
|
||||
logger.Debugf(1, "Loading the certificate %s", config.Config.IDPCertificateAuthority)
|
||||
err := appendCertificateFromFile(pool, config.Config.IDPCertificateAuthority)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not load the certificate of idp-certificate-authority: %w", err)
|
||||
}
|
||||
}
|
||||
if config.Config.IDPCertificateAuthorityData != "" {
|
||||
logger.Debugf(1, "Loading the certificate of idp-certificate-authority-data")
|
||||
err := appendEncodedCertificate(pool, config.Config.IDPCertificateAuthorityData)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not load the certificate of idp-certificate-authority-data: %w", err)
|
||||
}
|
||||
}
|
||||
if config.CACertFilename != "" {
|
||||
logger.Debugf(1, "Loading the certificate %s", config.CACertFilename)
|
||||
err := appendCertificateFromFile(pool, config.CACertFilename)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not load the certificate: %w", err)
|
||||
}
|
||||
}
|
||||
c := &tls.Config{
|
||||
InsecureSkipVerify: config.SkipTLSVerify,
|
||||
}
|
||||
if len(pool.Subjects()) > 0 {
|
||||
c.RootCAs = pool
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func appendCertificateFromFile(pool *x509.CertPool, filename string) error {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("could not read %s: %w", filename, err)
|
||||
}
|
||||
if !pool.AppendCertsFromPEM(b) {
|
||||
return xerrors.Errorf("could not append certificate from %s", filename)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendEncodedCertificate(pool *x509.CertPool, base64String string) error {
|
||||
b, err := base64.StdEncoding.DecodeString(base64String)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("could not decode base64: %w", err)
|
||||
}
|
||||
if !pool.AppendCertsFromPEM(b) {
|
||||
return xerrors.Errorf("could not append certificate")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
89
adaptors/oidc/tls/tls_test.go
Normal file
89
adaptors/oidc/tls/tls_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package tls
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/e2e_test/logger"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
)
|
||||
|
||||
func TestNewConfig(t *testing.T) {
|
||||
testingLogger := logger.New(t)
|
||||
testingLogger.SetLevel(1)
|
||||
|
||||
t.Run("Defaults", func(t *testing.T) {
|
||||
c, err := NewConfig(adaptors.OIDCClientConfig{}, testingLogger)
|
||||
if err != nil {
|
||||
t.Fatalf("NewConfig error: %+v", err)
|
||||
}
|
||||
if c.InsecureSkipVerify {
|
||||
t.Errorf("InsecureSkipVerify wants false but true")
|
||||
}
|
||||
if c.RootCAs != nil {
|
||||
t.Errorf("RootCAs wants nil but %+v", c.RootCAs)
|
||||
}
|
||||
})
|
||||
t.Run("SkipTLSVerify", func(t *testing.T) {
|
||||
config := adaptors.OIDCClientConfig{
|
||||
SkipTLSVerify: true,
|
||||
}
|
||||
c, err := NewConfig(config, testingLogger)
|
||||
if err != nil {
|
||||
t.Fatalf("NewConfig error: %+v", err)
|
||||
}
|
||||
if !c.InsecureSkipVerify {
|
||||
t.Errorf("InsecureSkipVerify wants true but false")
|
||||
}
|
||||
if c.RootCAs != nil {
|
||||
t.Errorf("RootCAs wants nil but %+v", c.RootCAs)
|
||||
}
|
||||
})
|
||||
t.Run("AllCertificates", func(t *testing.T) {
|
||||
config := adaptors.OIDCClientConfig{
|
||||
Config: kubeconfig.OIDCConfig{
|
||||
IDPCertificateAuthority: "testdata/ca1.crt",
|
||||
IDPCertificateAuthorityData: string(readFile(t, "testdata/ca2.crt.base64")),
|
||||
},
|
||||
CACertFilename: "testdata/ca3.crt",
|
||||
}
|
||||
c, err := NewConfig(config, testingLogger)
|
||||
if err != nil {
|
||||
t.Fatalf("NewConfig error: %+v", err)
|
||||
}
|
||||
if c.InsecureSkipVerify {
|
||||
t.Errorf("InsecureSkipVerify wants false but true")
|
||||
}
|
||||
if c.RootCAs == nil {
|
||||
t.Fatalf("RootCAs wants non-nil but nil")
|
||||
}
|
||||
subjects := c.RootCAs.Subjects()
|
||||
if len(subjects) != 3 {
|
||||
t.Errorf("len(subjects) wants 3 but %d", len(subjects))
|
||||
}
|
||||
})
|
||||
t.Run("InvalidCertificate", func(t *testing.T) {
|
||||
config := adaptors.OIDCClientConfig{
|
||||
Config: kubeconfig.OIDCConfig{
|
||||
IDPCertificateAuthority: "testdata/ca1.crt",
|
||||
IDPCertificateAuthorityData: string(readFile(t, "testdata/ca2.crt.base64")),
|
||||
},
|
||||
CACertFilename: "testdata/Makefile", // invalid cert
|
||||
}
|
||||
_, err := NewConfig(config, testingLogger)
|
||||
if err == nil {
|
||||
t.Fatalf("NewConfig wants non-nil but nil")
|
||||
}
|
||||
t.Logf("expected error: %+v", err)
|
||||
})
|
||||
}
|
||||
|
||||
func readFile(t *testing.T, filename string) []byte {
|
||||
t.Helper()
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile error: %s", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package authn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
oidc "github.com/coreos/go-oidc"
|
||||
"github.com/int128/kubelogin/authz"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// TokenSet is a set of tokens and claims.
|
||||
type TokenSet struct {
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
Claims *Claims
|
||||
}
|
||||
|
||||
// Claims represents properties in the ID token.
|
||||
type Claims struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// GetTokenSet retrieves a token from the OIDC provider.
|
||||
func GetTokenSet(ctx context.Context, issuer string, clientID string, clientSecret string) (*TokenSet, error) {
|
||||
provider, err := oidc.NewProvider(ctx, issuer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not access OIDC issuer: %s", err)
|
||||
}
|
||||
flow := authz.BrowserAuthCodeFlow{
|
||||
Port: 8000,
|
||||
Config: oauth2.Config{
|
||||
Endpoint: provider.Endpoint(),
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
Scopes: []string{oidc.ScopeOpenID, "email"},
|
||||
},
|
||||
}
|
||||
token, err := flow.GetToken(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not get a token: %s", err)
|
||||
}
|
||||
rawIDToken, ok := token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("id_token is missing in the token response: %s", token)
|
||||
}
|
||||
verifier := provider.Verifier(&oidc.Config{ClientID: clientID})
|
||||
idToken, err := verifier.Verify(ctx, rawIDToken)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not verify the id_token: %s", err)
|
||||
}
|
||||
var claims Claims
|
||||
if err := idToken.Claims(&claims); err != nil {
|
||||
return nil, fmt.Errorf("Could not extract claims from the token response: %s", err)
|
||||
}
|
||||
return &TokenSet{
|
||||
IDToken: rawIDToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
Claims: &claims,
|
||||
}, nil
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// Flow represents an authorization method.
|
||||
type Flow interface {
|
||||
GetToken(context.Context) (*oauth2.Token, error)
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// BrowserAuthCodeFlow is a flow to get a token by browser interaction.
|
||||
type BrowserAuthCodeFlow struct {
|
||||
oauth2.Config
|
||||
Port int // HTTP server port
|
||||
}
|
||||
|
||||
// GetToken returns a token.
|
||||
func (f *BrowserAuthCodeFlow) GetToken(ctx context.Context) (*oauth2.Token, error) {
|
||||
f.Config.RedirectURL = fmt.Sprintf("http://localhost:%d/", f.Port)
|
||||
state, err := generateOAuthState()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("Open http://localhost:%d for authorization", f.Port)
|
||||
code, err := f.getCode(ctx, &f.Config, state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token, err := f.Config.Exchange(ctx, code)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not exchange oauth code: %s", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (f *BrowserAuthCodeFlow) getCode(ctx context.Context, config *oauth2.Config, state string) (string, error) {
|
||||
codeCh := make(chan string)
|
||||
errCh := make(chan error)
|
||||
server := http.Server{
|
||||
Addr: fmt.Sprintf(":%d", f.Port),
|
||||
Handler: &handler{
|
||||
AuthCodeURL: config.AuthCodeURL(state),
|
||||
Callback: func(code string, actualState string, err error) {
|
||||
switch {
|
||||
case err != nil:
|
||||
errCh <- err
|
||||
case actualState != state:
|
||||
errCh <- fmt.Errorf("OAuth state did not match, should be %s but %s", state, actualState)
|
||||
default:
|
||||
codeCh <- code
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
go func() {
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
server.Shutdown(ctx)
|
||||
return "", err
|
||||
case code := <-codeCh:
|
||||
server.Shutdown(ctx)
|
||||
return code, nil
|
||||
}
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
AuthCodeURL string
|
||||
Callback func(code string, state string, err error)
|
||||
}
|
||||
|
||||
func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s %s", r.Method, r.RequestURI)
|
||||
switch r.URL.Path {
|
||||
case "/":
|
||||
code := r.URL.Query().Get("code")
|
||||
state := r.URL.Query().Get("state")
|
||||
errorCode := r.URL.Query().Get("error")
|
||||
errorDescription := r.URL.Query().Get("error_description")
|
||||
switch {
|
||||
case code != "":
|
||||
s.Callback(code, state, nil)
|
||||
fmt.Fprintf(w, "Back to command line.")
|
||||
case errorCode != "":
|
||||
s.Callback("", "", fmt.Errorf("OAuth Error: %s %s", errorCode, errorDescription))
|
||||
fmt.Fprintf(w, "Back to command line.")
|
||||
default:
|
||||
http.Redirect(w, r, s.AuthCodeURL, 302)
|
||||
}
|
||||
default:
|
||||
http.Error(w, "Not Found", 404)
|
||||
}
|
||||
}
|
||||
35
authz/cli.go
35
authz/cli.go
@@ -1,35 +0,0 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// CLIAuthCodeFlow is a flow to get a token by keyboard interaction.
|
||||
type CLIAuthCodeFlow struct {
|
||||
oauth2.Config
|
||||
}
|
||||
|
||||
// GetToken returns a token by browser interaction.
|
||||
func (f *CLIAuthCodeFlow) GetToken(ctx context.Context) (*oauth2.Token, error) {
|
||||
f.Config.RedirectURL = "urn:ietf:wg:oauth:2.0:oob"
|
||||
state, err := generateOAuthState()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authCodeURL := f.Config.AuthCodeURL(state)
|
||||
log.Printf("Open %s for authorization", authCodeURL)
|
||||
fmt.Print("Enter code: ")
|
||||
var code string
|
||||
if _, err := fmt.Scanln(&code); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token, err := f.Config.Exchange(ctx, code)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not exchange oauth code: %s", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func generateOAuthState() (string, error) {
|
||||
var n uint64
|
||||
if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%x", n), nil
|
||||
}
|
||||
46
di/di.go
Normal file
46
di/di.go
Normal file
@@ -0,0 +1,46 @@
|
||||
//+build wireinject
|
||||
|
||||
// Package di provides dependency injection.
|
||||
package di
|
||||
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/cmd"
|
||||
"github.com/int128/kubelogin/adaptors/env"
|
||||
"github.com/int128/kubelogin/adaptors/kubeconfig"
|
||||
"github.com/int128/kubelogin/adaptors/logger"
|
||||
"github.com/int128/kubelogin/adaptors/oidc"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/int128/kubelogin/usecases/auth"
|
||||
"github.com/int128/kubelogin/usecases/login"
|
||||
)
|
||||
|
||||
// NewCmd returns an instance of adaptors.Cmd.
|
||||
func NewCmd() adaptors.Cmd {
|
||||
wire.Build(
|
||||
auth.Set,
|
||||
auth.ExtraSet,
|
||||
login.Set,
|
||||
cmd.Set,
|
||||
env.Set,
|
||||
kubeconfig.Set,
|
||||
oidc.Set,
|
||||
logger.Set,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewCmdWith returns an instance of adaptors.Cmd with given dependencies.
|
||||
// This is only for e2e tests.
|
||||
func NewCmdWith(adaptors.Logger, usecases.LoginShowLocalServerURL) adaptors.Cmd {
|
||||
wire.Build(
|
||||
auth.Set,
|
||||
login.Set,
|
||||
cmd.Set,
|
||||
env.Set,
|
||||
kubeconfig.Set,
|
||||
oidc.Set,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
86
di/wire_gen.go
Normal file
86
di/wire_gen.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Code generated by Wire. DO NOT EDIT.
|
||||
|
||||
//go:generate wire
|
||||
//+build !wireinject
|
||||
|
||||
package di
|
||||
|
||||
import (
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/cmd"
|
||||
"github.com/int128/kubelogin/adaptors/env"
|
||||
"github.com/int128/kubelogin/adaptors/kubeconfig"
|
||||
"github.com/int128/kubelogin/adaptors/logger"
|
||||
"github.com/int128/kubelogin/adaptors/oidc"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/int128/kubelogin/usecases/auth"
|
||||
"github.com/int128/kubelogin/usecases/login"
|
||||
)
|
||||
|
||||
// Injectors from di.go:
|
||||
|
||||
func NewCmd() adaptors.Cmd {
|
||||
adaptorsLogger := logger.New()
|
||||
factory := &oidc.Factory{
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
envEnv := &env.Env{}
|
||||
showLocalServerURL := &auth.ShowLocalServerURL{
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
authentication := &auth.Authentication{
|
||||
OIDC: factory,
|
||||
Env: envEnv,
|
||||
Logger: adaptorsLogger,
|
||||
ShowLocalServerURL: showLocalServerURL,
|
||||
}
|
||||
kubeconfigKubeconfig := &kubeconfig.Kubeconfig{}
|
||||
loginLogin := &login.Login{
|
||||
Authentication: authentication,
|
||||
Kubeconfig: kubeconfigKubeconfig,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
exec := &login.Exec{
|
||||
Authentication: authentication,
|
||||
Kubeconfig: kubeconfigKubeconfig,
|
||||
Env: envEnv,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
cmdCmd := &cmd.Cmd{
|
||||
Login: loginLogin,
|
||||
LoginAndExec: exec,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
return cmdCmd
|
||||
}
|
||||
|
||||
func NewCmdWith(adaptorsLogger adaptors.Logger, loginShowLocalServerURL usecases.LoginShowLocalServerURL) adaptors.Cmd {
|
||||
factory := &oidc.Factory{
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
envEnv := &env.Env{}
|
||||
authentication := &auth.Authentication{
|
||||
OIDC: factory,
|
||||
Env: envEnv,
|
||||
Logger: adaptorsLogger,
|
||||
ShowLocalServerURL: loginShowLocalServerURL,
|
||||
}
|
||||
kubeconfigKubeconfig := &kubeconfig.Kubeconfig{}
|
||||
loginLogin := &login.Login{
|
||||
Authentication: authentication,
|
||||
Kubeconfig: kubeconfigKubeconfig,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
exec := &login.Exec{
|
||||
Authentication: authentication,
|
||||
Kubeconfig: kubeconfigKubeconfig,
|
||||
Env: envEnv,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
cmdCmd := &cmd.Cmd{
|
||||
Login: loginLogin,
|
||||
LoginAndExec: exec,
|
||||
Logger: adaptorsLogger,
|
||||
}
|
||||
return cmdCmd
|
||||
}
|
||||
BIN
docs/authn.png
Normal file
BIN
docs/authn.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 65 KiB |
16
docs/authn.seqdiag
Normal file
16
docs/authn.seqdiag
Normal file
@@ -0,0 +1,16 @@
|
||||
seqdiag {
|
||||
User -> kubelogin [label = "execute"];
|
||||
kubelogin -> Browser [label = "open"];
|
||||
Browser -> Provider [label = "authentication request"];
|
||||
Browser <-- Provider [label = "redirect"];
|
||||
User -> Browser [label = "enter credentials"];
|
||||
Browser -> Provider [label = "credentials"];
|
||||
Browser <-- Provider [label = "authentication response"];
|
||||
User <-- Browser [label = "success"];
|
||||
kubelogin <-- Browser [label = "close"];
|
||||
kubelogin -> Provider [label = "token request"];
|
||||
kubelogin <-- Provider [label = "token response"];
|
||||
kubelogin -> kubeconfig [label = "write token"];
|
||||
kubelogin <-- kubeconfig;
|
||||
User <-- kubelogin;
|
||||
}
|
||||
95
docs/google.md
Normal file
95
docs/google.md
Normal file
@@ -0,0 +1,95 @@
|
||||
# Getting Started with Google Identity Platform
|
||||
|
||||
## Prerequisite
|
||||
|
||||
- You have a Google account.
|
||||
- You have the Cluster Admin role of the Kubernetes cluster.
|
||||
- You can configure the Kubernetes API server.
|
||||
- `kubectl` and `kubelogin` are installed to your computer.
|
||||
|
||||
## 1. Setup Google API
|
||||
|
||||
Open [Google APIs Console](https://console.developers.google.com/apis/credentials) and create an OAuth client with the following setting:
|
||||
|
||||
- Application Type: Other
|
||||
|
||||
## 2. Setup Kubernetes API server
|
||||
|
||||
Configure your Kubernetes API Server accepts [OpenID Connect Tokens](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens).
|
||||
|
||||
If you are using [kops](https://github.com/kubernetes/kops), run `kops edit cluster` and append the following settings:
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
kubeAPIServer:
|
||||
oidcIssuerURL: https://accounts.google.com
|
||||
oidcClientID: YOUR_CLIENT_ID.apps.googleusercontent.com
|
||||
```
|
||||
|
||||
## 3. Setup Kubernetes cluster
|
||||
|
||||
Here assign the `cluster-admin` role to you.
|
||||
|
||||
```yaml
|
||||
kind: ClusterRoleBinding
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
metadata:
|
||||
name: oidc-admin-group
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: cluster-admin
|
||||
subjects:
|
||||
- kind: User
|
||||
name: https://accounts.google.com#1234567890
|
||||
```
|
||||
|
||||
You can create a custom role and assign it as well.
|
||||
|
||||
## 4. Setup kubectl
|
||||
|
||||
Configure `kubectl` for the OIDC authentication.
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials KUBECONTEXT \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://accounts.google.com \
|
||||
--auth-provider-arg client-id=YOUR_CLIENT_ID.apps.googleusercontent.com \
|
||||
--auth-provider-arg client-secret=YOUR_CLIENT_SECRET
|
||||
```
|
||||
|
||||
## 5. Run kubelogin
|
||||
|
||||
Run `kubelogin`.
|
||||
|
||||
```
|
||||
% kubelogin
|
||||
Open http://localhost:8000 for authentication
|
||||
You got a valid token until 2019-05-16 22:03:13 +0900 JST
|
||||
Updated ~/.kubeconfig
|
||||
```
|
||||
|
||||
Now your `~/.kube/config` should be like:
|
||||
|
||||
```yaml
|
||||
users:
|
||||
- name: hello.k8s.local
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
idp-issuer-url: https://accounts.google.com
|
||||
client-id: YOUR_CLIENT_ID.apps.googleusercontent.com
|
||||
client-secret: YOUR_SECRET
|
||||
id-token: ey... # kubelogin will update ID token here
|
||||
refresh-token: ey... # kubelogin will update refresh token here
|
||||
name: oidc
|
||||
```
|
||||
|
||||
Make sure you can access to the Kubernetes cluster.
|
||||
|
||||
```
|
||||
% kubectl get nodes
|
||||
NAME STATUS ROLES AGE VERSION
|
||||
ip-1-2-3-4.us-west-2.compute.internal Ready node 21d v1.9.6
|
||||
ip-1-2-3-5.us-west-2.compute.internal Ready node 20d v1.9.6
|
||||
```
|
||||
BIN
docs/keycloak-login.png
Normal file
BIN
docs/keycloak-login.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 278 KiB |
111
docs/keycloak.md
Normal file
111
docs/keycloak.md
Normal file
@@ -0,0 +1,111 @@
|
||||
# Getting Started with Keycloak
|
||||
|
||||
## Prerequisite
|
||||
|
||||
- You have an administrator role of the Keycloak realm.
|
||||
- You have an administrator role of the Kubernetes cluster.
|
||||
- You can configure the Kubernetes API server.
|
||||
- `kubectl` and `kubelogin` are installed.
|
||||
|
||||
## 1. Setup Keycloak
|
||||
|
||||
Open the Keycloak and create an OIDC client as follows:
|
||||
|
||||
- Client ID: `kubernetes`
|
||||
- Valid Redirect URLs:
|
||||
- `http://localhost:8000`
|
||||
- `http://localhost:18000` (used if the port 8000 is already in use)
|
||||
- Issuer URL: `https://keycloak.example.com/auth/realms/YOUR_REALM`
|
||||
|
||||
You can associate client roles by adding the following mapper:
|
||||
|
||||
- Name: `groups`
|
||||
- Mapper Type: `User Client Role`
|
||||
- Client ID: `kubernetes`
|
||||
- Client Role prefix: `kubernetes:`
|
||||
- Token Claim Name: `groups`
|
||||
- Add to ID token: on
|
||||
|
||||
For example, if you have the `admin` role of the client, you will get a JWT with the claim `{"groups": ["kubernetes:admin"]}`.
|
||||
|
||||
## 2. Setup Kubernetes API server
|
||||
|
||||
Configure your Kubernetes API server accepts [OpenID Connect Tokens](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens).
|
||||
|
||||
If you are using [kops](https://github.com/kubernetes/kops), run `kops edit cluster` and add the following spec:
|
||||
|
||||
```yaml
|
||||
spec:
|
||||
kubeAPIServer:
|
||||
oidcIssuerURL: https://keycloak.example.com/auth/realms/YOUR_REALM
|
||||
oidcClientID: kubernetes
|
||||
oidcGroupsClaim: groups
|
||||
```
|
||||
|
||||
## 3. Setup Kubernetes cluster
|
||||
|
||||
Here assign the `cluster-admin` role to the `kubernetes:admin` group.
|
||||
|
||||
```yaml
|
||||
kind: ClusterRoleBinding
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
metadata:
|
||||
name: keycloak-admin-group
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: cluster-admin
|
||||
subjects:
|
||||
- kind: Group
|
||||
name: kubernetes:admin
|
||||
```
|
||||
|
||||
You can create a custom role and assign it as well.
|
||||
|
||||
## 4. Setup kubectl
|
||||
|
||||
Configure `kubectl` for the OIDC authentication.
|
||||
|
||||
```sh
|
||||
kubectl config set-credentials KUBECONTEXT \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://keycloak.example.com/auth/realms/YOUR_REALM \
|
||||
--auth-provider-arg client-id=kubernetes \
|
||||
--auth-provider-arg client-secret=YOUR_CLIENT_SECRET
|
||||
```
|
||||
|
||||
## 5. Run kubelogin
|
||||
|
||||
Run `kubelogin`.
|
||||
|
||||
```
|
||||
% kubelogin
|
||||
Open http://localhost:8000 for authentication
|
||||
You got a valid token until 2019-05-16 22:03:13 +0900 JST
|
||||
Updated ~/.kubeconfig
|
||||
```
|
||||
|
||||
Now your `~/.kube/config` should be like:
|
||||
|
||||
```yaml
|
||||
users:
|
||||
- name: hello.k8s.local
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
idp-issuer-url: https://keycloak.example.com/auth/realms/YOUR_REALM
|
||||
client-id: kubernetes
|
||||
client-secret: YOUR_SECRET
|
||||
id-token: ey... # kubelogin will update ID token here
|
||||
refresh-token: ey... # kubelogin will update refresh token here
|
||||
name: oidc
|
||||
```
|
||||
|
||||
Make sure you can access to the Kubernetes cluster.
|
||||
|
||||
```
|
||||
% kubectl get nodes
|
||||
NAME STATUS ROLES AGE VERSION
|
||||
ip-1-2-3-4.us-west-2.compute.internal Ready node 21d v1.9.6
|
||||
ip-1-2-3-5.us-west-2.compute.internal Ready node 20d v1.9.6
|
||||
```
|
||||
40
docs/team_ops.md
Normal file
40
docs/team_ops.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Team Operation
|
||||
|
||||
## kops
|
||||
|
||||
Export the kubeconfig.
|
||||
|
||||
```sh
|
||||
KUBECONFIG=.kubeconfig kops export kubecfg hello.k8s.local
|
||||
```
|
||||
|
||||
Remove the `admin` access from the kubeconfig.
|
||||
It should look as like:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
certificate-authority-data: LS...
|
||||
server: https://api.hello.k8s.example.com
|
||||
name: hello.k8s.local
|
||||
contexts:
|
||||
- context:
|
||||
cluster: hello.k8s.local
|
||||
user: hello.k8s.local
|
||||
name: hello.k8s.local
|
||||
current-context: hello.k8s.local
|
||||
preferences: {}
|
||||
users:
|
||||
- name: hello.k8s.local
|
||||
user:
|
||||
auth-provider:
|
||||
name: oidc
|
||||
config:
|
||||
client-id: YOUR_CLIEND_ID
|
||||
client-secret: YOUR_CLIENT_SECRET
|
||||
idp-issuer-url: YOUR_ISSUER
|
||||
```
|
||||
|
||||
You can share the kubeconfig to your team members for easy onboarding.
|
||||
403
e2e_test/cmd_test.go
Normal file
403
e2e_test/cmd_test.go
Normal file
@@ -0,0 +1,403 @@
|
||||
package e2e_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/di"
|
||||
"github.com/int128/kubelogin/e2e_test/idp"
|
||||
"github.com/int128/kubelogin/e2e_test/idp/mock_idp"
|
||||
"github.com/int128/kubelogin/e2e_test/keys"
|
||||
"github.com/int128/kubelogin/e2e_test/kubeconfig"
|
||||
"github.com/int128/kubelogin/e2e_test/localserver"
|
||||
"github.com/int128/kubelogin/e2e_test/logger"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
)
|
||||
|
||||
// Run the integration tests.
|
||||
//
|
||||
// 1. Start the auth server.
|
||||
// 2. Run the Cmd.
|
||||
// 3. Open a request for the local server.
|
||||
// 4. Verify the kuneconfig.
|
||||
//
|
||||
func TestCmd_Run(t *testing.T) {
|
||||
timeout := 1 * time.Second
|
||||
|
||||
t.Run("Defaults", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "openid", &idToken)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{Issuer: serverURL})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
req := startBrowserRequest(t, ctx, nil)
|
||||
runCmd(t, ctx, req, "--kubeconfig", kubeConfigFilename, "--skip-open-browser", "--listen-port", "0")
|
||||
req.wait()
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("ResourceOwnerPasswordCredentials", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
idToken := newIDToken(t, serverURL, "", time.Hour)
|
||||
service.EXPECT().Discovery().Return(idp.NewDiscoveryResponse(serverURL))
|
||||
service.EXPECT().GetCertificates().Return(idp.NewCertificatesResponse(keys.JWSKeyPair))
|
||||
service.EXPECT().AuthenticatePassword("USER", "PASS", "openid").
|
||||
Return(idp.NewTokenResponse(idToken, "YOUR_REFRESH_TOKEN"), nil)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{Issuer: serverURL})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
runCmd(t, ctx, &nopBrowserRequest{t}, "--kubeconfig", kubeConfigFilename, "--skip-open-browser", "--username", "USER", "--password", "PASS")
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("env:KUBECONFIG", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "openid", &idToken)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{Issuer: serverURL})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
setenv(t, "KUBECONFIG", kubeConfigFilename+string(os.PathListSeparator)+"kubeconfig/testdata/dummy.yaml")
|
||||
defer unsetenv(t, "KUBECONFIG")
|
||||
|
||||
req := startBrowserRequest(t, ctx, nil)
|
||||
runCmd(t, ctx, req, "--skip-open-browser", "--listen-port", "0")
|
||||
req.wait()
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("ExtraScopes", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "profile groups openid", &idToken)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
ExtraScopes: "profile,groups",
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
req := startBrowserRequest(t, ctx, nil)
|
||||
runCmd(t, ctx, req, "--kubeconfig", kubeConfigFilename, "--skip-open-browser", "--listen-port", "0")
|
||||
req.wait()
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("CACert", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.StartTLS(t, keys.TLSServerCert, keys.TLSServerKey, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "openid", &idToken)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
IDPCertificateAuthority: keys.TLSCACert,
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
req := startBrowserRequest(t, ctx, keys.TLSCACertAsConfig)
|
||||
runCmd(t, ctx, req, "--kubeconfig", kubeConfigFilename, "--skip-open-browser", "--listen-port", "0")
|
||||
req.wait()
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("CACertData", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.StartTLS(t, keys.TLSServerCert, keys.TLSServerKey, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "openid", &idToken)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
IDPCertificateAuthorityData: keys.TLSCACertAsBase64,
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
req := startBrowserRequest(t, ctx, keys.TLSCACertAsConfig)
|
||||
runCmd(t, ctx, req, "--kubeconfig", kubeConfigFilename, "--skip-open-browser", "--listen-port", "0")
|
||||
req.wait()
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("HasValidToken", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
idToken := newIDToken(t, serverURL, "YOUR_NONCE", time.Hour)
|
||||
service.EXPECT().Discovery().Return(idp.NewDiscoveryResponse(serverURL))
|
||||
service.EXPECT().GetCertificates().Return(idp.NewCertificatesResponse(keys.JWSKeyPair))
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
runCmd(t, ctx, &nopBrowserRequest{t}, "--kubeconfig", kubeConfigFilename, "--skip-open-browser")
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("HasValidRefreshToken", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
idToken := newIDToken(t, serverURL, "YOUR_NONCE", time.Hour)
|
||||
service.EXPECT().Discovery().Return(idp.NewDiscoveryResponse(serverURL))
|
||||
service.EXPECT().GetCertificates().Return(idp.NewCertificatesResponse(keys.JWSKeyPair))
|
||||
service.EXPECT().Refresh("VALID_REFRESH_TOKEN").
|
||||
Return(idp.NewTokenResponse(idToken, "NEW_REFRESH_TOKEN"), nil)
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
IDToken: newIDToken(t, serverURL, "YOUR_NONCE", -time.Hour), // expired
|
||||
RefreshToken: "VALID_REFRESH_TOKEN",
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
runCmd(t, ctx, &nopBrowserRequest{t}, "--kubeconfig", kubeConfigFilename, "--skip-open-browser")
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "NEW_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("HasExpiredRefreshToken", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
service := mock_idp.NewMockService(ctrl)
|
||||
serverURL, server := localserver.Start(t, idp.NewHandler(t, service))
|
||||
defer server.Shutdown(t, ctx)
|
||||
var idToken string
|
||||
setupMockIDPForCodeFlow(t, service, serverURL, "openid", &idToken)
|
||||
service.EXPECT().Refresh("EXPIRED_REFRESH_TOKEN").
|
||||
Return(nil, &idp.ErrorResponse{Code: "invalid_request", Description: "token has expired"}).
|
||||
MaxTimes(2) // package oauth2 will retry refreshing the token
|
||||
|
||||
kubeConfigFilename := kubeconfig.Create(t, &kubeconfig.Values{
|
||||
Issuer: serverURL,
|
||||
IDToken: newIDToken(t, serverURL, "YOUR_NONCE", -time.Hour), // expired
|
||||
RefreshToken: "EXPIRED_REFRESH_TOKEN",
|
||||
})
|
||||
defer os.Remove(kubeConfigFilename)
|
||||
|
||||
req := startBrowserRequest(t, ctx, nil)
|
||||
runCmd(t, ctx, req, "--kubeconfig", kubeConfigFilename, "--skip-open-browser")
|
||||
kubeconfig.Verify(t, kubeConfigFilename, kubeconfig.AuthProviderConfig{
|
||||
IDToken: idToken,
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func newIDToken(t *testing.T, issuer, nonce string, expiration time.Duration) string {
|
||||
t.Helper()
|
||||
var claims struct {
|
||||
jwt.StandardClaims
|
||||
Nonce string `json:"nonce"`
|
||||
Groups []string `json:"groups"`
|
||||
}
|
||||
claims.StandardClaims = jwt.StandardClaims{
|
||||
Issuer: issuer,
|
||||
Audience: "kubernetes",
|
||||
Subject: "SUBJECT",
|
||||
IssuedAt: time.Now().Unix(),
|
||||
ExpiresAt: time.Now().Add(expiration).Unix(),
|
||||
}
|
||||
claims.Nonce = nonce
|
||||
claims.Groups = []string{"admin", "users"}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
||||
s, err := token.SignedString(keys.JWSKeyPair)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not sign the claims: %s", err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func setupMockIDPForCodeFlow(t *testing.T, service *mock_idp.MockService, serverURL, scope string, idToken *string) {
|
||||
var nonce string
|
||||
service.EXPECT().Discovery().Return(idp.NewDiscoveryResponse(serverURL))
|
||||
service.EXPECT().GetCertificates().Return(idp.NewCertificatesResponse(keys.JWSKeyPair))
|
||||
service.EXPECT().AuthenticateCode(scope, gomock.Any()).
|
||||
DoAndReturn(func(_, gotNonce string) (string, error) {
|
||||
nonce = gotNonce
|
||||
return "YOUR_AUTH_CODE", nil
|
||||
})
|
||||
service.EXPECT().Exchange("YOUR_AUTH_CODE").
|
||||
DoAndReturn(func(string) (*idp.TokenResponse, error) {
|
||||
*idToken = newIDToken(t, serverURL, nonce, time.Hour)
|
||||
return idp.NewTokenResponse(*idToken, "YOUR_REFRESH_TOKEN"), nil
|
||||
})
|
||||
}
|
||||
|
||||
func runCmd(t *testing.T, ctx context.Context, s usecases.LoginShowLocalServerURL, args ...string) {
|
||||
t.Helper()
|
||||
cmd := di.NewCmdWith(logger.New(t), s)
|
||||
exitCode := cmd.Run(ctx, append([]string{"kubelogin", "--v=1"}, args...), "HEAD")
|
||||
if exitCode != 0 {
|
||||
t.Errorf("exit status wants 0 but %d", exitCode)
|
||||
}
|
||||
}
|
||||
|
||||
type nopBrowserRequest struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (r *nopBrowserRequest) ShowLocalServerURL(url string) {
|
||||
r.t.Errorf("ShowLocalServerURL must not be called")
|
||||
}
|
||||
|
||||
type browserRequest struct {
|
||||
t *testing.T
|
||||
urlCh chan<- string
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (r *browserRequest) ShowLocalServerURL(url string) {
|
||||
defer close(r.urlCh)
|
||||
r.t.Logf("Open %s for authentication", url)
|
||||
r.urlCh <- url
|
||||
}
|
||||
|
||||
func (r *browserRequest) wait() {
|
||||
r.wg.Wait()
|
||||
}
|
||||
|
||||
func startBrowserRequest(t *testing.T, ctx context.Context, tlsConfig *tls.Config) *browserRequest {
|
||||
t.Helper()
|
||||
urlCh := make(chan string)
|
||||
var wg sync.WaitGroup
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
select {
|
||||
case url := <-urlCh:
|
||||
client := http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
t.Errorf("could not create a request: %s", err)
|
||||
return
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Errorf("could not send a request: %s", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("StatusCode wants 200 but %d", resp.StatusCode)
|
||||
}
|
||||
case err := <-ctx.Done():
|
||||
t.Errorf("context done while waiting for URL prompt: %s", err)
|
||||
}
|
||||
}()
|
||||
wg.Add(1)
|
||||
return &browserRequest{t, urlCh, &wg}
|
||||
}
|
||||
|
||||
func setenv(t *testing.T, key, value string) {
|
||||
t.Helper()
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
t.Fatalf("Could not set the env var %s=%s: %s", key, value, err)
|
||||
}
|
||||
}
|
||||
|
||||
func unsetenv(t *testing.T, key string) {
|
||||
t.Helper()
|
||||
if err := os.Unsetenv(key); err != nil {
|
||||
t.Fatalf("Could not unset the env var %s: %s", key, err)
|
||||
}
|
||||
}
|
||||
142
e2e_test/idp/handler.go
Normal file
142
e2e_test/idp/handler.go
Normal file
@@ -0,0 +1,142 @@
|
||||
// Package idp provides a test double of the identity provider of OpenID Connect.
|
||||
package idp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func NewHandler(t *testing.T, service Service) *Handler {
|
||||
return &Handler{t, service}
|
||||
}
|
||||
|
||||
// Handler provides a HTTP handler for the identity provider of OpenID Connect.
|
||||
// You need to implement the Service interface.
|
||||
// Note that this skips some security checks and is only for testing.
|
||||
type Handler struct {
|
||||
t *testing.T
|
||||
service Service
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
wr := &responseWriterRecorder{w, 200}
|
||||
err := h.serveHTTP(wr, r)
|
||||
if err == nil {
|
||||
h.t.Logf("%d %s %s", wr.statusCode, r.Method, r.RequestURI)
|
||||
return
|
||||
}
|
||||
if errResp := new(ErrorResponse); xerrors.As(err, &errResp) {
|
||||
h.t.Logf("400 %s %s: %s", r.Method, r.RequestURI, err)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(errResp); err != nil {
|
||||
h.t.Errorf("idp/handler: could not write the response: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
h.t.Logf("500 %s %s: %s", r.Method, r.RequestURI, err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
}
|
||||
|
||||
type responseWriterRecorder struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func (w *responseWriterRecorder) WriteHeader(statusCode int) {
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
w.statusCode = statusCode
|
||||
}
|
||||
|
||||
func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
m := r.Method
|
||||
p := r.URL.Path
|
||||
switch {
|
||||
case m == "GET" && p == "/.well-known/openid-configuration":
|
||||
discoveryResponse := h.service.Discovery()
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(discoveryResponse); err != nil {
|
||||
return xerrors.Errorf("could not render json: %w", err)
|
||||
}
|
||||
case m == "GET" && p == "/certs":
|
||||
certificatesResponse := h.service.GetCertificates()
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(certificatesResponse); err != nil {
|
||||
return xerrors.Errorf("could not render json: %w", err)
|
||||
}
|
||||
case m == "GET" && p == "/auth":
|
||||
// 3.1.2.1. Authentication Request
|
||||
// https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
|
||||
q := r.URL.Query()
|
||||
redirectURI, scope, state, nonce := q.Get("redirect_uri"), q.Get("scope"), q.Get("state"), q.Get("nonce")
|
||||
code, err := h.service.AuthenticateCode(scope, nonce)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("authentication error: %w", err)
|
||||
}
|
||||
to := fmt.Sprintf("%s?state=%s&code=%s", redirectURI, state, code)
|
||||
http.Redirect(w, r, to, 302)
|
||||
case m == "POST" && p == "/token":
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return xerrors.Errorf("could not parse the form: %w", err)
|
||||
}
|
||||
grantType := r.Form.Get("grant_type")
|
||||
switch grantType {
|
||||
case "authorization_code":
|
||||
// 3.1.3.1. Token Request
|
||||
// https://openid.net/specs/openid-connect-core-1_0.html#TokenRequest
|
||||
code := r.Form.Get("code")
|
||||
tokenResponse, err := h.service.Exchange(code)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("token request error: %w", err)
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(tokenResponse); err != nil {
|
||||
return xerrors.Errorf("could not render json: %w", err)
|
||||
}
|
||||
case "password":
|
||||
// 4.3. Resource Owner Password Credentials Grant
|
||||
// https://tools.ietf.org/html/rfc6749#section-4.3
|
||||
username, password, scope := r.Form.Get("username"), r.Form.Get("password"), r.Form.Get("scope")
|
||||
tokenResponse, err := h.service.AuthenticatePassword(username, password, scope)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("authentication error: %w", err)
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(tokenResponse); err != nil {
|
||||
return xerrors.Errorf("could not render json: %w", err)
|
||||
}
|
||||
case "refresh_token":
|
||||
// 12.1. Refresh Request
|
||||
// https://openid.net/specs/openid-connect-core-1_0.html#RefreshingAccessToken
|
||||
refreshToken := r.Form.Get("refresh_token")
|
||||
tokenResponse, err := h.service.Refresh(refreshToken)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("token refresh error: %w", err)
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(tokenResponse); err != nil {
|
||||
return xerrors.Errorf("could not render json: %w", err)
|
||||
}
|
||||
default:
|
||||
// 5.2. Error Response
|
||||
// https://tools.ietf.org/html/rfc6749#section-5.2
|
||||
return &ErrorResponse{
|
||||
Code: "invalid_grant",
|
||||
Description: fmt.Sprintf("unknown grant_type %s", grantType),
|
||||
}
|
||||
}
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
110
e2e_test/idp/mock_idp/mock_service.go
Normal file
110
e2e_test/idp/mock_idp/mock_service.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/int128/kubelogin/e2e_test/idp (interfaces: Service)
|
||||
|
||||
// Package mock_idp is a generated GoMock package.
|
||||
package mock_idp
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
idp "github.com/int128/kubelogin/e2e_test/idp"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockService is a mock of Service interface
|
||||
type MockService struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockServiceMockRecorder
|
||||
}
|
||||
|
||||
// MockServiceMockRecorder is the mock recorder for MockService
|
||||
type MockServiceMockRecorder struct {
|
||||
mock *MockService
|
||||
}
|
||||
|
||||
// NewMockService creates a new mock instance
|
||||
func NewMockService(ctrl *gomock.Controller) *MockService {
|
||||
mock := &MockService{ctrl: ctrl}
|
||||
mock.recorder = &MockServiceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockService) EXPECT() *MockServiceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// AuthenticateCode mocks base method
|
||||
func (m *MockService) AuthenticateCode(arg0, arg1 string) (string, error) {
|
||||
ret := m.ctrl.Call(m, "AuthenticateCode", arg0, arg1)
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// AuthenticateCode indicates an expected call of AuthenticateCode
|
||||
func (mr *MockServiceMockRecorder) AuthenticateCode(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AuthenticateCode", reflect.TypeOf((*MockService)(nil).AuthenticateCode), arg0, arg1)
|
||||
}
|
||||
|
||||
// AuthenticatePassword mocks base method
|
||||
func (m *MockService) AuthenticatePassword(arg0, arg1, arg2 string) (*idp.TokenResponse, error) {
|
||||
ret := m.ctrl.Call(m, "AuthenticatePassword", arg0, arg1, arg2)
|
||||
ret0, _ := ret[0].(*idp.TokenResponse)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// AuthenticatePassword indicates an expected call of AuthenticatePassword
|
||||
func (mr *MockServiceMockRecorder) AuthenticatePassword(arg0, arg1, arg2 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AuthenticatePassword", reflect.TypeOf((*MockService)(nil).AuthenticatePassword), arg0, arg1, arg2)
|
||||
}
|
||||
|
||||
// Discovery mocks base method
|
||||
func (m *MockService) Discovery() *idp.DiscoveryResponse {
|
||||
ret := m.ctrl.Call(m, "Discovery")
|
||||
ret0, _ := ret[0].(*idp.DiscoveryResponse)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Discovery indicates an expected call of Discovery
|
||||
func (mr *MockServiceMockRecorder) Discovery() *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discovery", reflect.TypeOf((*MockService)(nil).Discovery))
|
||||
}
|
||||
|
||||
// Exchange mocks base method
|
||||
func (m *MockService) Exchange(arg0 string) (*idp.TokenResponse, error) {
|
||||
ret := m.ctrl.Call(m, "Exchange", arg0)
|
||||
ret0, _ := ret[0].(*idp.TokenResponse)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Exchange indicates an expected call of Exchange
|
||||
func (mr *MockServiceMockRecorder) Exchange(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Exchange", reflect.TypeOf((*MockService)(nil).Exchange), arg0)
|
||||
}
|
||||
|
||||
// GetCertificates mocks base method
|
||||
func (m *MockService) GetCertificates() *idp.CertificatesResponse {
|
||||
ret := m.ctrl.Call(m, "GetCertificates")
|
||||
ret0, _ := ret[0].(*idp.CertificatesResponse)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetCertificates indicates an expected call of GetCertificates
|
||||
func (mr *MockServiceMockRecorder) GetCertificates() *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCertificates", reflect.TypeOf((*MockService)(nil).GetCertificates))
|
||||
}
|
||||
|
||||
// Refresh mocks base method
|
||||
func (m *MockService) Refresh(arg0 string) (*idp.TokenResponse, error) {
|
||||
ret := m.ctrl.Call(m, "Refresh", arg0)
|
||||
ret0, _ := ret[0].(*idp.TokenResponse)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Refresh indicates an expected call of Refresh
|
||||
func (mr *MockServiceMockRecorder) Refresh(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Refresh", reflect.TypeOf((*MockService)(nil).Refresh), arg0)
|
||||
}
|
||||
120
e2e_test/idp/service.go
Normal file
120
e2e_test/idp/service.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package idp
|
||||
|
||||
//go:generate mockgen -destination mock_idp/mock_service.go github.com/int128/kubelogin/e2e_test/idp Service
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// Service provides discovery and authentication methods.
|
||||
// If an implemented method returns an ErrorResponse,
|
||||
// the handler will respond 400 and corresponding json of the ErrorResponse.
|
||||
// Otherwise, the handler will respond 500 and fail the current test.
|
||||
type Service interface {
|
||||
Discovery() *DiscoveryResponse
|
||||
GetCertificates() *CertificatesResponse
|
||||
AuthenticateCode(scope, nonce string) (code string, err error)
|
||||
Exchange(code string) (*TokenResponse, error)
|
||||
AuthenticatePassword(username, password, scope string) (*TokenResponse, error)
|
||||
Refresh(refreshToken string) (*TokenResponse, error)
|
||||
}
|
||||
|
||||
type DiscoveryResponse struct {
|
||||
Issuer string `json:"issuer"`
|
||||
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
||||
TokenEndpoint string `json:"token_endpoint"`
|
||||
UserinfoEndpoint string `json:"userinfo_endpoint"`
|
||||
RevocationEndpoint string `json:"revocation_endpoint"`
|
||||
JwksURI string `json:"jwks_uri"`
|
||||
ResponseTypesSupported []string `json:"response_types_supported"`
|
||||
SubjectTypesSupported []string `json:"subject_types_supported"`
|
||||
IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported"`
|
||||
ScopesSupported []string `json:"scopes_supported"`
|
||||
TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported"`
|
||||
ClaimsSupported []string `json:"claims_supported"`
|
||||
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
|
||||
}
|
||||
|
||||
// NewDiscoveryResponse returns a DiscoveryResponse for the local server.
|
||||
// This is based on https://accounts.google.com/.well-known/openid-configuration.
|
||||
func NewDiscoveryResponse(issuer string) *DiscoveryResponse {
|
||||
return &DiscoveryResponse{
|
||||
Issuer: issuer,
|
||||
AuthorizationEndpoint: issuer + "/auth",
|
||||
TokenEndpoint: issuer + "/token",
|
||||
JwksURI: issuer + "/certs",
|
||||
UserinfoEndpoint: issuer + "/userinfo",
|
||||
RevocationEndpoint: issuer + "/revoke",
|
||||
ResponseTypesSupported: []string{"code id_token"},
|
||||
SubjectTypesSupported: []string{"public"},
|
||||
IDTokenSigningAlgValuesSupported: []string{"RS256"},
|
||||
ScopesSupported: []string{"openid", "email", "profile"},
|
||||
TokenEndpointAuthMethodsSupported: []string{"client_secret_post", "client_secret_basic"},
|
||||
CodeChallengeMethodsSupported: []string{"plain", "S256"},
|
||||
ClaimsSupported: []string{"aud", "email", "exp", "iat", "iss", "name", "sub"},
|
||||
}
|
||||
}
|
||||
|
||||
type CertificatesResponse struct {
|
||||
Keys []*CertificatesResponseKey `json:"keys"`
|
||||
}
|
||||
|
||||
type CertificatesResponseKey struct {
|
||||
Kty string `json:"kty"`
|
||||
Alg string `json:"alg"`
|
||||
Use string `json:"use"`
|
||||
Kid string `json:"kid"`
|
||||
N string `json:"n"`
|
||||
E string `json:"e"`
|
||||
}
|
||||
|
||||
// NewCertificatesResponse returns a CertificatesResponse using the key pair.
|
||||
// This is used for verifying a signature of ID token.
|
||||
func NewCertificatesResponse(idTokenKeyPair *rsa.PrivateKey) *CertificatesResponse {
|
||||
return &CertificatesResponse{
|
||||
Keys: []*CertificatesResponseKey{
|
||||
{
|
||||
Kty: "RSA",
|
||||
Alg: "RS256",
|
||||
Use: "sig",
|
||||
Kid: "dummy",
|
||||
E: base64.RawURLEncoding.EncodeToString(big.NewInt(int64(idTokenKeyPair.E)).Bytes()),
|
||||
N: base64.RawURLEncoding.EncodeToString(idTokenKeyPair.N.Bytes()),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
IDToken string `json:"id_token"`
|
||||
}
|
||||
|
||||
// NewTokenResponse returns a TokenResponse.
|
||||
func NewTokenResponse(idToken, refreshToken string) *TokenResponse {
|
||||
return &TokenResponse{
|
||||
TokenType: "Bearer",
|
||||
ExpiresIn: 3600,
|
||||
AccessToken: "YOUR_ACCESS_TOKEN",
|
||||
IDToken: idToken,
|
||||
RefreshToken: refreshToken,
|
||||
}
|
||||
}
|
||||
|
||||
// ErrorResponse represents an error response described in the following section:
|
||||
// 5.2 Error Response
|
||||
// https://tools.ietf.org/html/rfc6749#section-5.2
|
||||
type ErrorResponse struct {
|
||||
Code string `json:"error"`
|
||||
Description string `json:"error_description"`
|
||||
}
|
||||
|
||||
func (err *ErrorResponse) Error() string {
|
||||
return fmt.Sprintf("%s(%s)", err.Code, err.Description)
|
||||
}
|
||||
71
e2e_test/keys/keys.go
Normal file
71
e2e_test/keys/keys.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"io/ioutil"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// TLSCACert is path to the CA certificate.
|
||||
// This should be generated by Makefile before test.
|
||||
const TLSCACert = "keys/testdata/ca.crt"
|
||||
|
||||
// TLSCACertAsBase64 is a base64 encoded string of TLSCACert.
|
||||
var TLSCACertAsBase64 string
|
||||
|
||||
// TLSCACertAsConfig is a TLS config including TLSCACert.
|
||||
var TLSCACertAsConfig = &tls.Config{RootCAs: x509.NewCertPool()}
|
||||
|
||||
// TLSServerCert is path to the server certificate.
|
||||
// This should be generated by Makefile before test.
|
||||
const TLSServerCert = "keys/testdata/server.crt"
|
||||
|
||||
// TLSServerKey is path to the server key.
|
||||
// This should be generated by Makefile before test.
|
||||
const TLSServerKey = "keys/testdata/server.key"
|
||||
|
||||
// JWSKey is path to the key for signing ID tokens.
|
||||
const JWSKey = "keys/testdata/jws.key"
|
||||
|
||||
// JWSKeyPair is the key pair loaded from JWSKey.
|
||||
var JWSKeyPair *rsa.PrivateKey
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
JWSKeyPair, err = readPrivateKey(JWSKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b, err := ioutil.ReadFile(TLSCACert)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
TLSCACertAsBase64 = base64.StdEncoding.EncodeToString(b)
|
||||
if !TLSCACertAsConfig.RootCAs.AppendCertsFromPEM(b) {
|
||||
panic("could not append the CA cert")
|
||||
}
|
||||
}
|
||||
|
||||
func readPrivateKey(name string) (*rsa.PrivateKey, error) {
|
||||
b, err := ioutil.ReadFile(name)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not read JWSKey: %w", err)
|
||||
}
|
||||
block, rest := pem.Decode(b)
|
||||
if block == nil {
|
||||
return nil, xerrors.New("could not decode PEM")
|
||||
}
|
||||
if len(rest) > 0 {
|
||||
return nil, xerrors.New("PEM should contain single key but multiple keys")
|
||||
}
|
||||
k, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not parse the key: %w", err)
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
4
e2e_test/keys/testdata/.gitignore
vendored
Normal file
4
e2e_test/keys/testdata/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/CA
|
||||
*.key
|
||||
*.csr
|
||||
*.crt
|
||||
53
e2e_test/keys/testdata/Makefile
vendored
Normal file
53
e2e_test/keys/testdata/Makefile
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
.PHONY: clean
|
||||
|
||||
all: server.crt ca.crt jws.key
|
||||
|
||||
clean:
|
||||
rm -v ca.* server.*
|
||||
|
||||
ca.key:
|
||||
openssl genrsa -out $@ 1024
|
||||
|
||||
ca.csr: openssl.cnf ca.key
|
||||
openssl req -config openssl.cnf \
|
||||
-new \
|
||||
-key ca.key \
|
||||
-subj "/CN=Hello CA" \
|
||||
-out $@
|
||||
openssl req -noout -text -in $@
|
||||
|
||||
ca.crt: ca.csr ca.key
|
||||
openssl x509 -req \
|
||||
-signkey ca.key \
|
||||
-in ca.csr \
|
||||
-out $@
|
||||
openssl x509 -text -in $@
|
||||
|
||||
server.key:
|
||||
openssl genrsa -out $@ 1024
|
||||
|
||||
server.csr: openssl.cnf server.key
|
||||
openssl req -config openssl.cnf \
|
||||
-new \
|
||||
-key server.key \
|
||||
-subj "/CN=localhost" \
|
||||
-out $@
|
||||
openssl req -noout -text -in $@
|
||||
|
||||
server.crt: openssl.cnf server.csr ca.key ca.crt
|
||||
rm -fr ./CA
|
||||
mkdir -p ./CA
|
||||
touch CA/index.txt
|
||||
touch CA/index.txt.attr
|
||||
echo 00 > CA/serial
|
||||
openssl ca -config openssl.cnf \
|
||||
-extensions v3_req \
|
||||
-batch \
|
||||
-cert ca.crt \
|
||||
-keyfile ca.key \
|
||||
-in server.csr \
|
||||
-out $@
|
||||
openssl x509 -text -in $@
|
||||
|
||||
jws.key:
|
||||
openssl genrsa -out $@ 1024
|
||||
37
e2e_test/keys/testdata/openssl.cnf
vendored
Normal file
37
e2e_test/keys/testdata/openssl.cnf
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
[ ca ]
|
||||
default_ca = CA_default
|
||||
|
||||
[ CA_default ]
|
||||
dir = ./CA
|
||||
certs = $dir
|
||||
crl_dir = $dir
|
||||
database = $dir/index.txt
|
||||
new_certs_dir = $dir
|
||||
default_md = sha256
|
||||
policy = policy_match
|
||||
serial = $dir/serial
|
||||
default_days = 365
|
||||
|
||||
[ policy_match ]
|
||||
countryName = optional
|
||||
stateOrProvinceName = optional
|
||||
organizationName = optional
|
||||
organizationalUnitName = optional
|
||||
commonName = supplied
|
||||
emailAddress = optional
|
||||
|
||||
[ req ]
|
||||
distinguished_name = req_distinguished_name
|
||||
req_extensions = v3_req
|
||||
x509_extensions = v3_ca
|
||||
|
||||
[ req_distinguished_name ]
|
||||
commonName = Common Name (e.g. server FQDN or YOUR name)
|
||||
|
||||
[ v3_req ]
|
||||
basicConstraints = CA:FALSE
|
||||
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
|
||||
subjectAltName = DNS:localhost
|
||||
|
||||
[ v3_ca ]
|
||||
basicConstraints = CA:true
|
||||
80
e2e_test/kubeconfig/kubeconfig.go
Normal file
80
e2e_test/kubeconfig/kubeconfig.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Values represents values in .kubeconfig template.
|
||||
type Values struct {
|
||||
Issuer string
|
||||
ExtraScopes string
|
||||
IDPCertificateAuthority string
|
||||
IDPCertificateAuthorityData string
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
}
|
||||
|
||||
// Create creates a kubeconfig file and returns path to it.
|
||||
func Create(t *testing.T, v *Values) string {
|
||||
t.Helper()
|
||||
f, err := ioutil.TempFile("", "kubeconfig")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
tpl, err := template.ParseFiles("kubeconfig/testdata/kubeconfig.yaml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tpl.Execute(f, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return f.Name()
|
||||
}
|
||||
|
||||
type AuthProviderConfig struct {
|
||||
IDToken string `yaml:"id-token"`
|
||||
RefreshToken string `yaml:"refresh-token"`
|
||||
}
|
||||
|
||||
// Verify returns true if the kubeconfig has valid values.
|
||||
func Verify(t *testing.T, kubeconfig string, want AuthProviderConfig) {
|
||||
t.Helper()
|
||||
f, err := os.Open(kubeconfig)
|
||||
if err != nil {
|
||||
t.Errorf("could not open kubeconfig: %s", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var y struct {
|
||||
Users []struct {
|
||||
User struct {
|
||||
AuthProvider struct {
|
||||
Config AuthProviderConfig `yaml:"config"`
|
||||
} `yaml:"auth-provider"`
|
||||
} `yaml:"user"`
|
||||
} `yaml:"users"`
|
||||
}
|
||||
d := yaml.NewDecoder(f)
|
||||
if err := d.Decode(&y); err != nil {
|
||||
t.Errorf("could not decode YAML: %s", err)
|
||||
return
|
||||
}
|
||||
if len(y.Users) != 1 {
|
||||
t.Errorf("len(users) wants 1 but %d", len(y.Users))
|
||||
return
|
||||
}
|
||||
currentConfig := y.Users[0].User.AuthProvider.Config
|
||||
if currentConfig.IDToken != want.IDToken {
|
||||
t.Errorf("id-token wants %s but %s", want.IDToken, currentConfig.IDToken)
|
||||
}
|
||||
if currentConfig.RefreshToken != want.RefreshToken {
|
||||
t.Errorf("refresh-token wants %s but %s", want.RefreshToken, currentConfig.RefreshToken)
|
||||
}
|
||||
}
|
||||
4
e2e_test/kubeconfig/testdata/dummy.yaml
vendored
Normal file
4
e2e_test/kubeconfig/testdata/dummy.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
current-context: dummy
|
||||
kind: Config
|
||||
preferences: {}
|
||||
37
e2e_test/kubeconfig/testdata/kubeconfig.yaml
vendored
Normal file
37
e2e_test/kubeconfig/testdata/kubeconfig.yaml
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://api.hello.k8s.example.com
|
||||
name: hello.k8s.local
|
||||
contexts:
|
||||
- context:
|
||||
cluster: hello.k8s.local
|
||||
user: hello.k8s.local
|
||||
name: hello.k8s.local
|
||||
current-context: hello.k8s.local
|
||||
preferences: {}
|
||||
users:
|
||||
- name: hello.k8s.local
|
||||
user:
|
||||
auth-provider:
|
||||
config:
|
||||
client-id: kubernetes
|
||||
client-secret: a3c508c3-73c9-42e2-ab14-487a1bf67c33
|
||||
idp-issuer-url: {{ .Issuer }}
|
||||
#{{ if .ExtraScopes }}
|
||||
extra-scopes: {{ .ExtraScopes }}
|
||||
#{{ end }}
|
||||
#{{ if .IDPCertificateAuthority }}
|
||||
idp-certificate-authority: {{ .IDPCertificateAuthority }}
|
||||
#{{ end }}
|
||||
#{{ if .IDPCertificateAuthorityData }}
|
||||
idp-certificate-authority-data: {{ .IDPCertificateAuthorityData }}
|
||||
#{{ end }}
|
||||
#{{ if .IDToken }}
|
||||
id-token: {{ .IDToken }}
|
||||
#{{ end }}
|
||||
#{{ if .RefreshToken }}
|
||||
refresh-token: {{ .RefreshToken }}
|
||||
#{{ end }}
|
||||
name: oidc
|
||||
76
e2e_test/localserver/authserver.go
Normal file
76
e2e_test/localserver/authserver.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Package localserver provides a http server running on localhost.
|
||||
// This is only for testing.
|
||||
//
|
||||
package localserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Shutdowner interface {
|
||||
Shutdown(t *testing.T, ctx context.Context)
|
||||
}
|
||||
|
||||
type shutdowner struct {
|
||||
l net.Listener
|
||||
s *http.Server
|
||||
}
|
||||
|
||||
func (s *shutdowner) Shutdown(t *testing.T, ctx context.Context) {
|
||||
// s.Shutdown() closes the lister as well,
|
||||
// so we do not need to call l.Close() explicitly
|
||||
if err := s.s.Shutdown(ctx); err != nil {
|
||||
t.Errorf("Could not shutdown the server: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts an authentication server.
|
||||
func Start(t *testing.T, h http.Handler) (string, Shutdowner) {
|
||||
t.Helper()
|
||||
l, port := newLocalhostListener(t)
|
||||
url := "http://localhost:" + port
|
||||
s := &http.Server{
|
||||
Handler: h,
|
||||
}
|
||||
go func() {
|
||||
err := s.Serve(l)
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
return url, &shutdowner{l, s}
|
||||
}
|
||||
|
||||
// Start starts an authentication server with TLS.
|
||||
func StartTLS(t *testing.T, cert string, key string, h http.Handler) (string, Shutdowner) {
|
||||
t.Helper()
|
||||
l, port := newLocalhostListener(t)
|
||||
url := "https://localhost:" + port
|
||||
s := &http.Server{
|
||||
Handler: h,
|
||||
}
|
||||
go func() {
|
||||
err := s.ServeTLS(l, cert, key)
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
return url, &shutdowner{l, s}
|
||||
}
|
||||
|
||||
func newLocalhostListener(t *testing.T) (net.Listener, string) {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create a listener: %s", err)
|
||||
}
|
||||
addr := l.Addr().String()
|
||||
_, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not parse the address %s: %s", addr, err)
|
||||
}
|
||||
return l, port
|
||||
}
|
||||
27
e2e_test/logger/logger.go
Normal file
27
e2e_test/logger/logger.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"github.com/int128/kubelogin/adaptors/logger"
|
||||
)
|
||||
|
||||
func New(t testingLogger) *logger.Logger {
|
||||
b := &bridge{t}
|
||||
return logger.NewWith(b, b)
|
||||
}
|
||||
|
||||
type testingLogger interface {
|
||||
Logf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
type bridge struct {
|
||||
t testingLogger
|
||||
}
|
||||
|
||||
func (b *bridge) Printf(format string, v ...interface{}) {
|
||||
b.t.Logf(format, v...)
|
||||
}
|
||||
|
||||
func (b *bridge) Output(calldepth int, s string) error {
|
||||
b.t.Logf("%s", s)
|
||||
return nil
|
||||
}
|
||||
32
go.mod
Normal file
32
go.mod
Normal file
@@ -0,0 +1,32 @@
|
||||
module github.com/int128/kubelogin
|
||||
|
||||
require (
|
||||
github.com/coreos/go-oidc v2.0.0+incompatible
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/go-test/deep v1.0.1
|
||||
github.com/gogo/protobuf v1.2.1 // indirect
|
||||
github.com/golang/mock v1.3.1
|
||||
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
|
||||
github.com/google/wire v0.3.0
|
||||
github.com/imdario/mergo v0.3.7 // indirect
|
||||
github.com/int128/oauth2cli v1.4.1
|
||||
github.com/json-iterator/go v1.1.6 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/spf13/pflag v1.0.3
|
||||
github.com/stretchr/testify v1.3.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd
|
||||
golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/square/go-jose.v2 v2.3.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
k8s.io/api v0.0.0-20190222213804-5cb15d344471 // indirect
|
||||
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628 // indirect
|
||||
k8s.io/client-go v10.0.0+incompatible
|
||||
k8s.io/klog v0.2.0 // indirect
|
||||
sigs.k8s.io/yaml v1.1.0 // indirect
|
||||
)
|
||||
133
go.sum
Normal file
133
go.sum
Normal file
@@ -0,0 +1,133 @@
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
|
||||
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
|
||||
github.com/coreos/go-oidc v2.0.0+incompatible h1:+RStIopZ8wooMx+Vs5Bt8zMXxV1ABl5LbakNExNmZIg=
|
||||
github.com/coreos/go-oidc v2.0.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
|
||||
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg=
|
||||
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
|
||||
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
|
||||
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
|
||||
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
|
||||
github.com/google/wire v0.3.0 h1:imGQZGEVEHpje5056+K+cgdO72p0LQv2xIIFXNGUf60=
|
||||
github.com/google/wire v0.3.0/go.mod h1:i1DMg/Lu8Sz5yYl25iOdmc5CT5qusaa+zmRWs16741s=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
|
||||
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/int128/oauth2cli v1.4.1 h1:IsaYMafEDS1jyArxYdmksw+nMsNxiYCQzdkPj3QF9BY=
|
||||
github.com/int128/oauth2cli v1.4.1/go.mod h1:CMJjyUSgKiobye1M/9byFACOjtB2LRo2mo7boklEKlI=
|
||||
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
|
||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
|
||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAmPf4YLrFg6oQUotqHQeUNWwkvo7jZp1GLU=
|
||||
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
|
||||
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
|
||||
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
|
||||
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
|
||||
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
|
||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
|
||||
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd h1:sMHc2rZHuzQmrbVoSpt9HgerkXPyIeCSO6k0zUMGfFk=
|
||||
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914 h1:jIOcLT9BZzyJ9ce+IwwZ+aF9yeCqzrR+NrD68a/SHKw=
|
||||
golang.org/x/oauth2 v0.0.0-20190319182350-c85d3e98c914/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
|
||||
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||
gopkg.in/square/go-jose.v2 v2.3.0 h1:nLzhkFyl5bkblqYBoiWJUt5JkWOzmiaBtCxdJAqJd3U=
|
||||
gopkg.in/square/go-jose.v2 v2.3.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
k8s.io/api v0.0.0-20190222213804-5cb15d344471 h1:MzQGt8qWQCR+39kbYRd0uQqsvSidpYqJLFeWiJ9l4OE=
|
||||
k8s.io/api v0.0.0-20190222213804-5cb15d344471/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
|
||||
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628 h1:UYfHH+KEF88OTg+GojQUwFTNxbxwmoktLwutUzR0GPg=
|
||||
k8s.io/apimachinery v0.0.0-20190221213512-86fb29eff628/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
|
||||
k8s.io/client-go v10.0.0+incompatible h1:+xQQxwjrcIPWDMJBAS+1G2FNk1McoPnb53xkvcDiDqE=
|
||||
k8s.io/client-go v10.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
|
||||
k8s.io/klog v0.2.0 h1:0ElL0OHzF3N+OhoJTL0uca20SxtYt4X4+bzHeqrB83c=
|
||||
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
|
||||
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
|
||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
@@ -1,52 +0,0 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
// FindCurrentAuthInfo returns the authInfo of current context.
|
||||
// If the current context does not exist, this returns nil.
|
||||
func FindCurrentAuthInfo(config *api.Config) *api.AuthInfo {
|
||||
context := config.Contexts[config.CurrentContext]
|
||||
if context == nil {
|
||||
return nil
|
||||
}
|
||||
return config.AuthInfos[context.AuthInfo]
|
||||
}
|
||||
|
||||
func ToOIDCAuthProviderConfig(authInfo *api.AuthInfo) (*OIDCAuthProviderConfig, error) {
|
||||
if authInfo.AuthProvider == nil {
|
||||
return nil, fmt.Errorf("auth-provider is not set, did you setup kubectl as listed here: https://github.com/int128/kubelogin#3-setup-kubectl")
|
||||
}
|
||||
if authInfo.AuthProvider.Name != "oidc" {
|
||||
return nil, fmt.Errorf("auth-provider `%s` is not supported", authInfo.AuthProvider.Name)
|
||||
}
|
||||
return (*OIDCAuthProviderConfig)(authInfo.AuthProvider), nil
|
||||
}
|
||||
|
||||
type OIDCAuthProviderConfig api.AuthProviderConfig
|
||||
|
||||
// IDPIssuerURL returns the idp-issuer-url.
|
||||
func (c *OIDCAuthProviderConfig) IDPIssuerURL() string {
|
||||
return c.Config["idp-issuer-url"]
|
||||
}
|
||||
|
||||
// ClientID returns the client-id.
|
||||
func (c *OIDCAuthProviderConfig) ClientID() string {
|
||||
return c.Config["client-id"]
|
||||
}
|
||||
|
||||
// ClientSecret returns the client-secret.
|
||||
func (c *OIDCAuthProviderConfig) ClientSecret() string {
|
||||
return c.Config["client-secret"]
|
||||
}
|
||||
|
||||
func (c *OIDCAuthProviderConfig) SetIDToken(idToken string) {
|
||||
c.Config["id-token"] = idToken
|
||||
}
|
||||
|
||||
func (c *OIDCAuthProviderConfig) SetRefreshToken(refreshToken string) {
|
||||
c.Config["refresh-token"] = refreshToken
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
const userKubeConfig = "~/.kube/config"
|
||||
|
||||
// Find returns path to the kubeconfig file,
|
||||
// that is given by env:KUBECONFIG or ~/.kube/config.
|
||||
// This returns an error if it is not found or I/O error occurred.
|
||||
func Find() (string, error) {
|
||||
path := os.Getenv("KUBECONFIG")
|
||||
if path == "" {
|
||||
var err error
|
||||
path, err = homedir.Expand(userKubeConfig)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Could not expand %s: %s", userKubeConfig, err)
|
||||
}
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Could not stat %s: %s", userKubeConfig, err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return "", fmt.Errorf("%s should be a file", userKubeConfig)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// Load loads the file and returns the Config.
|
||||
func Load(path string) (*api.Config, error) {
|
||||
config, err := clientcmd.LoadFromFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not load kubeconfig from %s: %s", path, err)
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Write writes the config to the file.
|
||||
func Write(config *api.Config, path string) error {
|
||||
return clientcmd.WriteToFile(*config, path)
|
||||
}
|
||||
15
kubelogin.rb
Normal file
15
kubelogin.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Kubelogin < Formula
|
||||
desc "A kubectl plugin for Kubernetes OpenID Connect authentication"
|
||||
homepage "https://github.com/int128/kubelogin"
|
||||
url "https://github.com/int128/kubelogin/releases/download/{{ env "VERSION" }}/kubelogin_darwin_amd64.zip"
|
||||
version "{{ env "VERSION" }}"
|
||||
sha256 "{{ sha256 .darwin_amd64_archive }}"
|
||||
def install
|
||||
bin.install "kubelogin" => "kubelogin"
|
||||
ln_s bin/"kubelogin", bin/"kubectl-oidc_login"
|
||||
end
|
||||
test do
|
||||
system "#{bin}/kubelogin -h"
|
||||
system "#{bin}/kubectl-oidc_login -h"
|
||||
end
|
||||
end
|
||||
37
main.go
37
main.go
@@ -2,40 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/int128/kubelogin/authn"
|
||||
"github.com/int128/kubelogin/kubeconfig"
|
||||
"github.com/int128/kubelogin/di"
|
||||
)
|
||||
|
||||
var version = "HEAD"
|
||||
|
||||
func main() {
|
||||
path, err := kubeconfig.Find()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not find kubeconfig: %s", err)
|
||||
}
|
||||
log.Printf("Reading %s", path)
|
||||
cfg, err := kubeconfig.Load(path)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not load kubeconfig: %s", err)
|
||||
}
|
||||
log.Printf("Using current context: %s", cfg.CurrentContext)
|
||||
authInfo := kubeconfig.FindCurrentAuthInfo(cfg)
|
||||
if authInfo == nil {
|
||||
log.Fatalf("Could not find current context: %s", cfg.CurrentContext)
|
||||
}
|
||||
authProvider, err := kubeconfig.ToOIDCAuthProviderConfig(authInfo)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not find auth-provider: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
token, err := authn.GetTokenSet(ctx, authProvider.IDPIssuerURL(), authProvider.ClientID(), authProvider.ClientSecret())
|
||||
if err != nil {
|
||||
log.Fatalf("Authentication error: %s", err)
|
||||
}
|
||||
|
||||
authProvider.SetIDToken(token.IDToken)
|
||||
authProvider.SetRefreshToken(token.RefreshToken)
|
||||
kubeconfig.Write(cfg, path)
|
||||
log.Printf("Updated %s", path)
|
||||
os.Exit(di.NewCmd().Run(context.Background(), os.Args, version))
|
||||
}
|
||||
|
||||
28
models/kubeconfig/kubeconfig.go
Normal file
28
models/kubeconfig/kubeconfig.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package kubeconfig
|
||||
|
||||
// ContextName represents name of a context.
|
||||
type ContextName string
|
||||
|
||||
// UserName represents name of a user.
|
||||
type UserName string
|
||||
|
||||
// AuthProvider represents the authentication provider,
|
||||
// i.e. context, user and auth-provider in a kubeconfig.
|
||||
type AuthProvider struct {
|
||||
LocationOfOrigin string // Path to the kubeconfig file which contains the user
|
||||
UserName UserName // User name
|
||||
ContextName ContextName // Context name (optional)
|
||||
OIDCConfig OIDCConfig
|
||||
}
|
||||
|
||||
// OIDCConfig represents a configuration of an OIDC provider.
|
||||
type OIDCConfig struct {
|
||||
IDPIssuerURL string // idp-issuer-url
|
||||
ClientID string // client-id
|
||||
ClientSecret string // client-secret
|
||||
IDPCertificateAuthority string // (optional) idp-certificate-authority
|
||||
IDPCertificateAuthorityData string // (optional) idp-certificate-authority-data
|
||||
ExtraScopes []string // (optional) extra-scopes
|
||||
IDToken string // (optional) id-token
|
||||
RefreshToken string // (optional) refresh-token
|
||||
}
|
||||
57
oidc-login.yaml
Normal file
57
oidc-login.yaml
Normal file
@@ -0,0 +1,57 @@
|
||||
apiVersion: krew.googlecontainertools.github.com/v1alpha2
|
||||
kind: Plugin
|
||||
metadata:
|
||||
name: oidc-login
|
||||
spec:
|
||||
shortDescription: Login for OpenID Connect authentication
|
||||
description: |
|
||||
This plugin gets a token from the OIDC provider and writes it to the kubeconfig.
|
||||
|
||||
Just run:
|
||||
% kubectl oidc-login
|
||||
|
||||
It opens the browser and you can log in to the provider.
|
||||
After authentication, it gets an ID token and refresh token and writes them to the kubeconfig.
|
||||
|
||||
caveats: |
|
||||
You need to setup the following components:
|
||||
* OIDC provider
|
||||
* Kubernetes API server
|
||||
* Role for your group or user
|
||||
* kubectl authentication
|
||||
|
||||
See https://github.com/int128/kubelogin for more.
|
||||
|
||||
homepage: https://github.com/int128/kubelogin
|
||||
version: {{ env "VERSION" }}
|
||||
platforms:
|
||||
- uri: https://github.com/int128/kubelogin/releases/download/{{ env "VERSION" }}/kubelogin_linux_amd64.zip
|
||||
sha256: "{{ sha256 .linux_amd64_archive }}"
|
||||
bin: kubelogin
|
||||
files:
|
||||
- from: "kubelogin"
|
||||
to: "."
|
||||
selector:
|
||||
matchLabels:
|
||||
os: linux
|
||||
arch: amd64
|
||||
- uri: https://github.com/int128/kubelogin/releases/download/{{ env "VERSION" }}/kubelogin_darwin_amd64.zip
|
||||
sha256: "{{ sha256 .darwin_amd64_archive }}"
|
||||
bin: kubelogin
|
||||
files:
|
||||
- from: "kubelogin"
|
||||
to: "."
|
||||
selector:
|
||||
matchLabels:
|
||||
os: darwin
|
||||
arch: amd64
|
||||
- uri: https://github.com/int128/kubelogin/releases/download/{{ env "VERSION" }}/kubelogin_windows_amd64.zip
|
||||
sha256: "{{ sha256 .windows_amd64_archive }}"
|
||||
bin: kubelogin.exe
|
||||
files:
|
||||
- from: "kubelogin.exe"
|
||||
to: "."
|
||||
selector:
|
||||
matchLabels:
|
||||
os: windows
|
||||
arch: amd64
|
||||
139
usecases/auth/auth.go
Normal file
139
usecases/auth/auth.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Set provides the use-case of Authentication.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Authentication), "*"),
|
||||
wire.Bind(new(usecases.Authentication), new(*Authentication)),
|
||||
)
|
||||
|
||||
// ExtraSet is a set of interaction components for e2e testing.
|
||||
var ExtraSet = wire.NewSet(
|
||||
wire.Struct(new(ShowLocalServerURL), "*"),
|
||||
wire.Bind(new(usecases.LoginShowLocalServerURL), new(*ShowLocalServerURL)),
|
||||
)
|
||||
|
||||
const passwordPrompt = "Password: "
|
||||
|
||||
// Authentication provides the internal use-case of authentication.
|
||||
//
|
||||
// If the IDToken is not set, it performs the authentication flow.
|
||||
// If the IDToken is valid, it does nothing.
|
||||
// If the IDtoken has expired and the RefreshToken is set, it refreshes the token.
|
||||
// If the RefreshToken has expired, it performs the authentication flow.
|
||||
//
|
||||
// The authentication flow is determined as:
|
||||
//
|
||||
// If the Username is not set, it performs the authorization code flow.
|
||||
// Otherwise, it performs the resource owner password credentials flow.
|
||||
// If the Password is not set, it asks a password by the prompt.
|
||||
//
|
||||
type Authentication struct {
|
||||
OIDC adaptors.OIDC
|
||||
Env adaptors.Env
|
||||
Logger adaptors.Logger
|
||||
ShowLocalServerURL usecases.LoginShowLocalServerURL
|
||||
}
|
||||
|
||||
func (u *Authentication) Do(ctx context.Context, in usecases.AuthenticationIn) (*usecases.AuthenticationOut, error) {
|
||||
client, err := u.OIDC.New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
CACertFilename: in.CACertFilename,
|
||||
SkipTLSVerify: in.SkipTLSVerify,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not create an OIDC client: %w", err)
|
||||
}
|
||||
|
||||
if in.CurrentAuthProvider.OIDCConfig.IDToken != "" {
|
||||
u.Logger.Debugf(1, "Verifying the token in the kubeconfig")
|
||||
out, err := client.Verify(ctx, adaptors.OIDCVerifyIn{IDToken: in.CurrentAuthProvider.OIDCConfig.IDToken})
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("invalid ID token in the kubeconfig, you need to remove it manually: %w", err)
|
||||
}
|
||||
if out.IDTokenExpiry.After(time.Now()) { //TODO: inject time service
|
||||
u.Logger.Debugf(1, "You already have a valid token in the kubeconfig")
|
||||
return &usecases.AuthenticationOut{
|
||||
AlreadyHasValidIDToken: true,
|
||||
IDToken: in.CurrentAuthProvider.OIDCConfig.IDToken,
|
||||
RefreshToken: in.CurrentAuthProvider.OIDCConfig.RefreshToken,
|
||||
IDTokenExpiry: out.IDTokenExpiry,
|
||||
IDTokenClaims: out.IDTokenClaims,
|
||||
}, nil
|
||||
}
|
||||
u.Logger.Debugf(1, "You have an expired token at %s", out.IDTokenExpiry)
|
||||
}
|
||||
|
||||
if in.CurrentAuthProvider.OIDCConfig.RefreshToken != "" {
|
||||
u.Logger.Debugf(1, "Refreshing the token")
|
||||
out, err := client.Refresh(ctx, adaptors.OIDCRefreshIn{
|
||||
RefreshToken: in.CurrentAuthProvider.OIDCConfig.RefreshToken,
|
||||
})
|
||||
if err == nil {
|
||||
return &usecases.AuthenticationOut{
|
||||
IDToken: out.IDToken,
|
||||
RefreshToken: out.RefreshToken,
|
||||
IDTokenExpiry: out.IDTokenExpiry,
|
||||
IDTokenClaims: out.IDTokenClaims,
|
||||
}, nil
|
||||
}
|
||||
u.Logger.Debugf(1, "Could not refresh the token: %s", err)
|
||||
}
|
||||
|
||||
if in.Username == "" {
|
||||
u.Logger.Debugf(1, "Performing the authentication code flow")
|
||||
out, err := client.AuthenticateByCode(ctx, adaptors.OIDCAuthenticateByCodeIn{
|
||||
LocalServerPort: in.ListenPort,
|
||||
SkipOpenBrowser: in.SkipOpenBrowser,
|
||||
ShowLocalServerURL: u.ShowLocalServerURL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("error while the authorization code flow: %w", err)
|
||||
}
|
||||
return &usecases.AuthenticationOut{
|
||||
IDToken: out.IDToken,
|
||||
RefreshToken: out.RefreshToken,
|
||||
IDTokenExpiry: out.IDTokenExpiry,
|
||||
IDTokenClaims: out.IDTokenClaims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
u.Logger.Debugf(1, "Performing the resource owner password credentials flow")
|
||||
if in.Password == "" {
|
||||
in.Password, err = u.Env.ReadPassword(passwordPrompt)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not read a password: %w", err)
|
||||
}
|
||||
}
|
||||
out, err := client.AuthenticateByPassword(ctx, adaptors.OIDCAuthenticateByPasswordIn{
|
||||
Username: in.Username,
|
||||
Password: in.Password,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("error while the resource owner password credentials flow: %w", err)
|
||||
}
|
||||
return &usecases.AuthenticationOut{
|
||||
IDToken: out.IDToken,
|
||||
RefreshToken: out.RefreshToken,
|
||||
IDTokenExpiry: out.IDTokenExpiry,
|
||||
IDTokenClaims: out.IDTokenClaims,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ShowLocalServerURL just shows the URL of local server to console.
|
||||
type ShowLocalServerURL struct {
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (s *ShowLocalServerURL) ShowLocalServerURL(url string) {
|
||||
s.Logger.Printf("Open %s for authentication", url)
|
||||
}
|
||||
382
usecases/auth/auth_test.go
Normal file
382
usecases/auth/auth_test.go
Normal file
@@ -0,0 +1,382 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-test/deep"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/adaptors/mock_adaptors"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func TestAuthentication_Do(t *testing.T) {
|
||||
dummyTokenClaims := map[string]string{"sub": "YOUR_SUBJECT"}
|
||||
pastTime := time.Now().Add(-time.Hour) //TODO: inject time service
|
||||
futureTime := time.Now().Add(time.Hour) //TODO: inject time service
|
||||
|
||||
t.Run("AuthorizationCodeFlow", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
ListenPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
AuthenticateByCode(ctx, adaptors.OIDCAuthenticateByCodeIn{
|
||||
LocalServerPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
}).
|
||||
Return(&adaptors.OIDCAuthenticateOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ResourceOwnerPasswordCredentialsFlow/UsePassword", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
AuthenticateByPassword(ctx, adaptors.OIDCAuthenticateByPasswordIn{
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
}).
|
||||
Return(&adaptors.OIDCAuthenticateOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ResourceOwnerPasswordCredentialsFlow/AskPassword", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
Username: "USER",
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
AuthenticateByPassword(ctx, adaptors.OIDCAuthenticateByPasswordIn{
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
}).
|
||||
Return(&adaptors.OIDCAuthenticateOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
mockEnv := mock_adaptors.NewMockEnv(ctrl)
|
||||
mockEnv.EXPECT().ReadPassword(passwordPrompt).Return("PASS", nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Env: mockEnv,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ResourceOwnerPasswordCredentialsFlow/AskPasswordError", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
Username: "USER",
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
}).
|
||||
Return(mock_adaptors.NewMockOIDCClient(ctrl), nil)
|
||||
mockEnv := mock_adaptors.NewMockEnv(ctrl)
|
||||
mockEnv.EXPECT().ReadPassword(passwordPrompt).Return("", xerrors.New("error"))
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Env: mockEnv,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
if out != nil {
|
||||
t.Errorf("out wants nil but %+v", out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HasValidIDToken", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
Verify(ctx, adaptors.OIDCVerifyIn{IDToken: "VALID_ID_TOKEN"}).
|
||||
Return(&adaptors.OIDCVerifyOut{
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
AlreadyHasValidIDToken: true,
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HasValidRefreshToken", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "EXPIRED_ID_TOKEN",
|
||||
RefreshToken: "VALID_REFRESH_TOKEN",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
Verify(ctx, adaptors.OIDCVerifyIn{IDToken: "EXPIRED_ID_TOKEN"}).
|
||||
Return(&adaptors.OIDCVerifyOut{
|
||||
IDTokenExpiry: pastTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDCClient.EXPECT().
|
||||
Refresh(ctx, adaptors.OIDCRefreshIn{
|
||||
RefreshToken: "VALID_REFRESH_TOKEN",
|
||||
}).
|
||||
Return(&adaptors.OIDCAuthenticateOut{
|
||||
IDToken: "NEW_ID_TOKEN",
|
||||
RefreshToken: "NEW_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
IDToken: "NEW_ID_TOKEN",
|
||||
RefreshToken: "NEW_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HasExpiredRefreshToken", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.AuthenticationIn{
|
||||
ListenPort: []int{10000},
|
||||
CurrentAuthProvider: &kubeconfig.AuthProvider{
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "EXPIRED_ID_TOKEN",
|
||||
RefreshToken: "EXPIRED_REFRESH_TOKEN",
|
||||
},
|
||||
},
|
||||
}
|
||||
mockOIDCClient := mock_adaptors.NewMockOIDCClient(ctrl)
|
||||
mockOIDCClient.EXPECT().
|
||||
Verify(ctx, adaptors.OIDCVerifyIn{IDToken: "EXPIRED_ID_TOKEN"}).
|
||||
Return(&adaptors.OIDCVerifyOut{
|
||||
IDTokenExpiry: pastTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDCClient.EXPECT().
|
||||
Refresh(ctx, adaptors.OIDCRefreshIn{
|
||||
RefreshToken: "EXPIRED_REFRESH_TOKEN",
|
||||
}).
|
||||
Return(nil, xerrors.New("token has expired"))
|
||||
mockOIDCClient.EXPECT().
|
||||
AuthenticateByCode(ctx, adaptors.OIDCAuthenticateByCodeIn{
|
||||
LocalServerPort: []int{10000},
|
||||
}).
|
||||
Return(&adaptors.OIDCAuthenticateOut{
|
||||
IDToken: "NEW_ID_TOKEN",
|
||||
RefreshToken: "NEW_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
mockOIDC := mock_adaptors.NewMockOIDC(ctrl)
|
||||
mockOIDC.EXPECT().
|
||||
New(ctx, adaptors.OIDCClientConfig{
|
||||
Config: in.CurrentAuthProvider.OIDCConfig,
|
||||
}).
|
||||
Return(mockOIDCClient, nil)
|
||||
u := Authentication{
|
||||
OIDC: mockOIDC,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.AuthenticationOut{
|
||||
IDToken: "NEW_ID_TOKEN",
|
||||
RefreshToken: "NEW_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
72
usecases/interfaces.go
Normal file
72
usecases/interfaces.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package usecases
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
)
|
||||
|
||||
//go:generate mockgen -destination mock_usecases/mock_usecases.go github.com/int128/kubelogin/usecases Login,LoginAndExec,Authentication
|
||||
|
||||
type Login interface {
|
||||
Do(ctx context.Context, in LoginIn) error
|
||||
}
|
||||
|
||||
// LoginIn represents an input DTO of the Login use-case.
|
||||
type LoginIn struct {
|
||||
KubeconfigFilename string // Default to the environment variable or global config as kubectl
|
||||
KubeconfigContext kubeconfig.ContextName // Default to the current context but ignored if KubeconfigUser is set
|
||||
KubeconfigUser kubeconfig.UserName // Default to the user of the context
|
||||
SkipOpenBrowser bool
|
||||
ListenPort []int
|
||||
Username string // If set, perform the resource owner password credentials grant
|
||||
Password string // If empty, read a password using Env.ReadPassword()
|
||||
CACertFilename string // If set, use the CA cert
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
// LoginShowLocalServerURL provides an interface to notify the URL of local server.
|
||||
// It is needed for the end-to-end tests.
|
||||
type LoginShowLocalServerURL interface {
|
||||
ShowLocalServerURL(url string)
|
||||
}
|
||||
|
||||
type LoginAndExec interface {
|
||||
Do(ctx context.Context, in LoginAndExecIn) (*LoginAndExecOut, error)
|
||||
}
|
||||
|
||||
// LoginAndExecInIn represents an input DTO of the LoginAndExec use-case.
|
||||
type LoginAndExecIn struct {
|
||||
LoginIn LoginIn
|
||||
Executable string
|
||||
Args []string
|
||||
}
|
||||
|
||||
type LoginAndExecOut struct {
|
||||
ExitCode int
|
||||
}
|
||||
|
||||
type Authentication interface {
|
||||
Do(ctx context.Context, in AuthenticationIn) (*AuthenticationOut, error)
|
||||
}
|
||||
|
||||
// AuthenticationIn represents an input DTO of the Authentication use-case.
|
||||
type AuthenticationIn struct {
|
||||
CurrentAuthProvider *kubeconfig.AuthProvider
|
||||
SkipOpenBrowser bool
|
||||
ListenPort []int
|
||||
Username string // If set, perform the resource owner password credentials grant
|
||||
Password string // If empty, read a password using Env.ReadPassword()
|
||||
CACertFilename string // If set, use the CA cert
|
||||
SkipTLSVerify bool
|
||||
}
|
||||
|
||||
// AuthenticationIn represents an output DTO of the Authentication use-case.
|
||||
type AuthenticationOut struct {
|
||||
AlreadyHasValidIDToken bool
|
||||
IDTokenExpiry time.Time
|
||||
IDTokenClaims map[string]string
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
}
|
||||
77
usecases/login/exec.go
Normal file
77
usecases/login/exec.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Exec provide the use case of transparently executing kubectl.
|
||||
//
|
||||
// If the current auth provider is not oidc, just run kubectl.
|
||||
// If the kubeconfig has a valid token, just run kubectl.
|
||||
// Otherwise, update the kubeconfig and run kubectl.
|
||||
//
|
||||
type Exec struct {
|
||||
Authentication usecases.Authentication
|
||||
Kubeconfig adaptors.Kubeconfig
|
||||
Env adaptors.Env
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (u *Exec) Do(ctx context.Context, in usecases.LoginAndExecIn) (*usecases.LoginAndExecOut, error) {
|
||||
if err := u.login(ctx, in.LoginIn); err != nil {
|
||||
return nil, xerrors.Errorf("could not log in to the provider: %w", err)
|
||||
}
|
||||
u.Logger.Debugf(1, "Executing the command %s %s", in.Executable, in.Args)
|
||||
exitCode, err := u.Env.Exec(ctx, in.Executable, in.Args)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not execute kubectl: %w", err)
|
||||
}
|
||||
u.Logger.Debugf(1, "The command exited with status %d", exitCode)
|
||||
return &usecases.LoginAndExecOut{ExitCode: exitCode}, nil
|
||||
}
|
||||
|
||||
func (u *Exec) login(ctx context.Context, in usecases.LoginIn) error {
|
||||
u.Logger.Debugf(1, "WARNING: log may contain your secrets such as token or password")
|
||||
|
||||
authProvider, err := u.Kubeconfig.GetCurrentAuthProvider(in.KubeconfigFilename, in.KubeconfigContext, in.KubeconfigUser)
|
||||
if err != nil {
|
||||
u.Logger.Debugf(1, "The current authentication provider is not oidc: %s", err)
|
||||
return nil
|
||||
}
|
||||
u.Logger.Debugf(1, "Using the authentication provider of the user %s", authProvider.UserName)
|
||||
u.Logger.Debugf(1, "A token will be written to %s", authProvider.LocationOfOrigin)
|
||||
|
||||
out, err := u.Authentication.Do(ctx, usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: authProvider,
|
||||
SkipOpenBrowser: in.SkipOpenBrowser,
|
||||
ListenPort: in.ListenPort,
|
||||
Username: in.Username,
|
||||
Password: in.Password,
|
||||
CACertFilename: in.CACertFilename,
|
||||
SkipTLSVerify: in.SkipTLSVerify,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("error while authentication: %w", err)
|
||||
}
|
||||
for k, v := range out.IDTokenClaims {
|
||||
u.Logger.Debugf(1, "ID token has the claim: %s=%v", k, v)
|
||||
}
|
||||
if out.AlreadyHasValidIDToken {
|
||||
u.Logger.Printf("You already have a valid token until %s", out.IDTokenExpiry)
|
||||
return nil
|
||||
}
|
||||
|
||||
u.Logger.Printf("You got a valid token until %s", out.IDTokenExpiry)
|
||||
authProvider.OIDCConfig.IDToken = out.IDToken
|
||||
authProvider.OIDCConfig.RefreshToken = out.RefreshToken
|
||||
|
||||
u.Logger.Debugf(1, "Writing the ID token and refresh token to %s", authProvider.LocationOfOrigin)
|
||||
if err := u.Kubeconfig.UpdateAuthProvider(authProvider); err != nil {
|
||||
return xerrors.Errorf("could not write the token to the kubeconfig: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
283
usecases/login/exec_test.go
Normal file
283
usecases/login/exec_test.go
Normal file
@@ -0,0 +1,283 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-test/deep"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors/mock_adaptors"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/int128/kubelogin/usecases/mock_usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func TestExec_Do(t *testing.T) {
|
||||
dummyTokenClaims := map[string]string{"sub": "YOUR_SUBJECT"}
|
||||
futureTime := time.Now().Add(time.Hour) //TODO: inject time service
|
||||
|
||||
t.Run("FullOptions", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginAndExecIn{
|
||||
Executable: "kubectl",
|
||||
Args: []string{"foo", "bar"},
|
||||
LoginIn: usecases.LoginIn{
|
||||
KubeconfigFilename: "/path/to/kubeconfig",
|
||||
KubeconfigContext: "theContext",
|
||||
KubeconfigUser: "theUser",
|
||||
ListenPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
},
|
||||
}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("/path/to/kubeconfig", kubeconfig.ContextName("theContext"), kubeconfig.UserName("theUser")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockKubeconfig.EXPECT().
|
||||
UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
})
|
||||
mockEnv := mock_adaptors.NewMockEnv(ctrl)
|
||||
mockEnv.EXPECT().
|
||||
Exec(ctx, "kubectl", []string{"foo", "bar"}).
|
||||
Return(123, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: currentAuthProvider,
|
||||
ListenPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Exec{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Env: mockEnv,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.LoginAndExecOut{
|
||||
ExitCode: 123,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HasValidIDToken", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginAndExecIn{
|
||||
Executable: "kubectl",
|
||||
Args: []string{"foo", "bar"},
|
||||
}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
},
|
||||
}
|
||||
mockEnv := mock_adaptors.NewMockEnv(ctrl)
|
||||
mockEnv.EXPECT().
|
||||
Exec(ctx, "kubectl", []string{"foo", "bar"}).
|
||||
Return(0, nil)
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
AlreadyHasValidIDToken: true,
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Exec{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Env: mockEnv,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.LoginAndExecOut{
|
||||
ExitCode: 0,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoOIDCConfig", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginAndExecIn{
|
||||
Executable: "kubectl",
|
||||
Args: []string{"foo", "bar"},
|
||||
LoginIn: usecases.LoginIn{},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(nil, xerrors.New("no oidc config"))
|
||||
mockEnv := mock_adaptors.NewMockEnv(ctrl)
|
||||
mockEnv.EXPECT().
|
||||
Exec(ctx, "kubectl", []string{"foo", "bar"}).
|
||||
Return(0, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
u := Exec{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Env: mockEnv,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
want := &usecases.LoginAndExecOut{
|
||||
ExitCode: 0,
|
||||
}
|
||||
if diff := deep.Equal(want, out); diff != nil {
|
||||
t.Error(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("AuthenticationError", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginAndExecIn{}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(nil, xerrors.New("authentication error"))
|
||||
u := Exec{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Env: mock_adaptors.NewMockEnv(ctrl),
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
if out != nil {
|
||||
t.Errorf("out wants nil but %+v", out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WriteError", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginAndExecIn{}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockKubeconfig.EXPECT().
|
||||
UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
}).
|
||||
Return(xerrors.New("I/O error"))
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Exec{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Env: mock_adaptors.NewMockEnv(ctrl),
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
out, err := u.Do(ctx, in)
|
||||
if err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
if out != nil {
|
||||
t.Errorf("out wants nil but %+v", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
78
usecases/login/login.go
Normal file
78
usecases/login/login.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/int128/kubelogin/adaptors"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Set provides the use-cases of logging in.
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(Login), "*"),
|
||||
wire.Struct(new(Exec), "*"),
|
||||
wire.Bind(new(usecases.Login), new(*Login)),
|
||||
wire.Bind(new(usecases.LoginAndExec), new(*Exec)),
|
||||
)
|
||||
|
||||
const oidcConfigErrorMessage = `No OIDC configuration found. Did you setup kubectl for OIDC authentication?
|
||||
kubectl config set-credentials CONTEXT_NAME \
|
||||
--auth-provider oidc \
|
||||
--auth-provider-arg idp-issuer-url=https://issuer.example.com \
|
||||
--auth-provider-arg client-id=YOUR_CLIENT_ID \
|
||||
--auth-provider-arg client-secret=YOUR_CLIENT_SECRET`
|
||||
|
||||
// Login provides the use case of explicit login.
|
||||
//
|
||||
// If the current auth provider is not oidc, show the error.
|
||||
// If the kubeconfig has a valid token, do nothing.
|
||||
// Otherwise, update the kubeconfig.
|
||||
//
|
||||
type Login struct {
|
||||
Authentication usecases.Authentication
|
||||
Kubeconfig adaptors.Kubeconfig
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (u *Login) Do(ctx context.Context, in usecases.LoginIn) error {
|
||||
u.Logger.Debugf(1, "WARNING: log may contain your secrets such as token or password")
|
||||
|
||||
authProvider, err := u.Kubeconfig.GetCurrentAuthProvider(in.KubeconfigFilename, in.KubeconfigContext, in.KubeconfigUser)
|
||||
if err != nil {
|
||||
u.Logger.Printf(oidcConfigErrorMessage)
|
||||
return xerrors.Errorf("could not find the current authentication provider: %w", err)
|
||||
}
|
||||
u.Logger.Debugf(1, "Using the authentication provider of the user %s", authProvider.UserName)
|
||||
u.Logger.Debugf(1, "A token will be written to %s", authProvider.LocationOfOrigin)
|
||||
|
||||
out, err := u.Authentication.Do(ctx, usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: authProvider,
|
||||
SkipOpenBrowser: in.SkipOpenBrowser,
|
||||
ListenPort: in.ListenPort,
|
||||
Username: in.Username,
|
||||
Password: in.Password,
|
||||
CACertFilename: in.CACertFilename,
|
||||
SkipTLSVerify: in.SkipTLSVerify,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("error while authentication: %w", err)
|
||||
}
|
||||
for k, v := range out.IDTokenClaims {
|
||||
u.Logger.Debugf(1, "ID token has the claim: %s=%v", k, v)
|
||||
}
|
||||
if out.AlreadyHasValidIDToken {
|
||||
u.Logger.Printf("You already have a valid token until %s", out.IDTokenExpiry)
|
||||
return nil
|
||||
}
|
||||
|
||||
u.Logger.Printf("You got a valid token until %s", out.IDTokenExpiry)
|
||||
authProvider.OIDCConfig.IDToken = out.IDToken
|
||||
authProvider.OIDCConfig.RefreshToken = out.RefreshToken
|
||||
u.Logger.Debugf(1, "Writing the ID token and refresh token to %s", authProvider.LocationOfOrigin)
|
||||
if err := u.Kubeconfig.UpdateAuthProvider(authProvider); err != nil {
|
||||
return xerrors.Errorf("could not write the token to the kubeconfig: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
225
usecases/login/login_test.go
Normal file
225
usecases/login/login_test.go
Normal file
@@ -0,0 +1,225 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/int128/kubelogin/adaptors/mock_adaptors"
|
||||
"github.com/int128/kubelogin/models/kubeconfig"
|
||||
"github.com/int128/kubelogin/usecases"
|
||||
"github.com/int128/kubelogin/usecases/mock_usecases"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func TestLogin_Do(t *testing.T) {
|
||||
dummyTokenClaims := map[string]string{"sub": "YOUR_SUBJECT"}
|
||||
futureTime := time.Now().Add(time.Hour) //TODO: inject time service
|
||||
|
||||
t.Run("FullOptions", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginIn{
|
||||
KubeconfigFilename: "/path/to/kubeconfig",
|
||||
KubeconfigContext: "theContext",
|
||||
KubeconfigUser: "theUser",
|
||||
ListenPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("/path/to/kubeconfig", kubeconfig.ContextName("theContext"), kubeconfig.UserName("theUser")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockKubeconfig.EXPECT().
|
||||
UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
})
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{
|
||||
CurrentAuthProvider: currentAuthProvider,
|
||||
ListenPort: []int{10000},
|
||||
SkipOpenBrowser: true,
|
||||
Username: "USER",
|
||||
Password: "PASS",
|
||||
CACertFilename: "/path/to/cert",
|
||||
SkipTLSVerify: true,
|
||||
}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Login{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
if err := u.Do(ctx, in); err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HasValidIDToken", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginIn{}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "theUser",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
AlreadyHasValidIDToken: true,
|
||||
IDToken: "VALID_ID_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Login{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
if err := u.Do(ctx, in); err != nil {
|
||||
t.Errorf("Do returned error: %+v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoOIDCConfig", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginIn{}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(nil, xerrors.New("no oidc config"))
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
u := Login{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
if err := u.Do(ctx, in); err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("AuthenticationError", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginIn{}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(nil, xerrors.New("authentication error"))
|
||||
u := Login{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
if err := u.Do(ctx, in); err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WriteError", func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
ctx := context.TODO()
|
||||
in := usecases.LoginIn{}
|
||||
currentAuthProvider := &kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
},
|
||||
}
|
||||
mockKubeconfig := mock_adaptors.NewMockKubeconfig(ctrl)
|
||||
mockKubeconfig.EXPECT().
|
||||
GetCurrentAuthProvider("", kubeconfig.ContextName(""), kubeconfig.UserName("")).
|
||||
Return(currentAuthProvider, nil)
|
||||
mockKubeconfig.EXPECT().
|
||||
UpdateAuthProvider(&kubeconfig.AuthProvider{
|
||||
LocationOfOrigin: "/path/to/kubeconfig",
|
||||
UserName: "google",
|
||||
OIDCConfig: kubeconfig.OIDCConfig{
|
||||
IDPIssuerURL: "https://accounts.google.com",
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
},
|
||||
}).
|
||||
Return(xerrors.New("I/O error"))
|
||||
mockAuthentication := mock_usecases.NewMockAuthentication(ctrl)
|
||||
mockAuthentication.EXPECT().
|
||||
Do(ctx, usecases.AuthenticationIn{CurrentAuthProvider: currentAuthProvider}).
|
||||
Return(&usecases.AuthenticationOut{
|
||||
IDToken: "YOUR_ID_TOKEN",
|
||||
RefreshToken: "YOUR_REFRESH_TOKEN",
|
||||
IDTokenExpiry: futureTime,
|
||||
IDTokenClaims: dummyTokenClaims,
|
||||
}, nil)
|
||||
u := Login{
|
||||
Authentication: mockAuthentication,
|
||||
Kubeconfig: mockKubeconfig,
|
||||
Logger: mock_adaptors.NewLogger(t, ctrl),
|
||||
}
|
||||
if err := u.Do(ctx, in); err == nil {
|
||||
t.Errorf("err wants non-nil but nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
119
usecases/mock_usecases/mock_usecases.go
Normal file
119
usecases/mock_usecases/mock_usecases.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/int128/kubelogin/usecases (interfaces: Login,LoginAndExec,Authentication)
|
||||
|
||||
// Package mock_usecases is a generated GoMock package.
|
||||
package mock_usecases
|
||||
|
||||
import (
|
||||
context "context"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
usecases "github.com/int128/kubelogin/usecases"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockLogin is a mock of Login interface
|
||||
type MockLogin struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockLoginMockRecorder
|
||||
}
|
||||
|
||||
// MockLoginMockRecorder is the mock recorder for MockLogin
|
||||
type MockLoginMockRecorder struct {
|
||||
mock *MockLogin
|
||||
}
|
||||
|
||||
// NewMockLogin creates a new mock instance
|
||||
func NewMockLogin(ctrl *gomock.Controller) *MockLogin {
|
||||
mock := &MockLogin{ctrl: ctrl}
|
||||
mock.recorder = &MockLoginMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockLogin) EXPECT() *MockLoginMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Do mocks base method
|
||||
func (m *MockLogin) Do(arg0 context.Context, arg1 usecases.LoginIn) error {
|
||||
ret := m.ctrl.Call(m, "Do", arg0, arg1)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Do indicates an expected call of Do
|
||||
func (mr *MockLoginMockRecorder) Do(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockLogin)(nil).Do), arg0, arg1)
|
||||
}
|
||||
|
||||
// MockLoginAndExec is a mock of LoginAndExec interface
|
||||
type MockLoginAndExec struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockLoginAndExecMockRecorder
|
||||
}
|
||||
|
||||
// MockLoginAndExecMockRecorder is the mock recorder for MockLoginAndExec
|
||||
type MockLoginAndExecMockRecorder struct {
|
||||
mock *MockLoginAndExec
|
||||
}
|
||||
|
||||
// NewMockLoginAndExec creates a new mock instance
|
||||
func NewMockLoginAndExec(ctrl *gomock.Controller) *MockLoginAndExec {
|
||||
mock := &MockLoginAndExec{ctrl: ctrl}
|
||||
mock.recorder = &MockLoginAndExecMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockLoginAndExec) EXPECT() *MockLoginAndExecMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Do mocks base method
|
||||
func (m *MockLoginAndExec) Do(arg0 context.Context, arg1 usecases.LoginAndExecIn) (*usecases.LoginAndExecOut, error) {
|
||||
ret := m.ctrl.Call(m, "Do", arg0, arg1)
|
||||
ret0, _ := ret[0].(*usecases.LoginAndExecOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Do indicates an expected call of Do
|
||||
func (mr *MockLoginAndExecMockRecorder) Do(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockLoginAndExec)(nil).Do), arg0, arg1)
|
||||
}
|
||||
|
||||
// MockAuthentication is a mock of Authentication interface
|
||||
type MockAuthentication struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockAuthenticationMockRecorder
|
||||
}
|
||||
|
||||
// MockAuthenticationMockRecorder is the mock recorder for MockAuthentication
|
||||
type MockAuthenticationMockRecorder struct {
|
||||
mock *MockAuthentication
|
||||
}
|
||||
|
||||
// NewMockAuthentication creates a new mock instance
|
||||
func NewMockAuthentication(ctrl *gomock.Controller) *MockAuthentication {
|
||||
mock := &MockAuthentication{ctrl: ctrl}
|
||||
mock.recorder = &MockAuthenticationMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockAuthentication) EXPECT() *MockAuthenticationMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Do mocks base method
|
||||
func (m *MockAuthentication) Do(arg0 context.Context, arg1 usecases.AuthenticationIn) (*usecases.AuthenticationOut, error) {
|
||||
ret := m.ctrl.Call(m, "Do", arg0, arg1)
|
||||
ret0, _ := ret[0].(*usecases.AuthenticationOut)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Do indicates an expected call of Do
|
||||
func (mr *MockAuthenticationMockRecorder) Do(arg0, arg1 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockAuthentication)(nil).Do), arg0, arg1)
|
||||
}
|
||||
Reference in New Issue
Block a user