mirror of
https://github.com/int128/kubelogin.git
synced 2026-08-23 21:06:15 +00:00
Refactor: extract adaptors.Logger
This commit is contained in:
+7
-6
@@ -3,8 +3,8 @@ package adaptors
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors/interfaces"
|
||||
"github.com/int128/kubelogin/usecases/interfaces"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
@@ -12,7 +12,8 @@ import (
|
||||
)
|
||||
|
||||
type Cmd struct {
|
||||
Login usecases.Login
|
||||
Login usecases.Login
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (cmd *Cmd) Run(ctx context.Context, args []string, version string) int {
|
||||
@@ -23,16 +24,16 @@ func (cmd *Cmd) Run(ctx context.Context, args []string, version string) int {
|
||||
version)
|
||||
args, err := parser.ParseArgs(args[1:])
|
||||
if err != nil {
|
||||
log.Printf("Error: %s", err)
|
||||
cmd.Logger.Logf("Error: %s", err)
|
||||
return 1
|
||||
}
|
||||
if len(args) > 0 {
|
||||
log.Printf("Error: too many arguments")
|
||||
cmd.Logger.Logf("Error: too many arguments")
|
||||
return 1
|
||||
}
|
||||
kubeConfig, err := o.ExpandKubeConfig()
|
||||
if err != nil {
|
||||
log.Printf("Error: invalid option: %s", err)
|
||||
cmd.Logger.Logf("Error: invalid option: %s", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ func (cmd *Cmd) Run(ctx context.Context, args []string, version string) int {
|
||||
SkipOpenBrowser: o.SkipOpenBrowser,
|
||||
}
|
||||
if err := cmd.Login.Do(ctx, in); err != nil {
|
||||
log.Printf("Error: %s", err)
|
||||
cmd.Logger.Logf("Error: %s", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
|
||||
@@ -27,7 +27,8 @@ func TestCmd_Run(t *testing.T) {
|
||||
})
|
||||
|
||||
cmd := Cmd{
|
||||
Login: login,
|
||||
Login: login,
|
||||
Logger: t,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable}, version)
|
||||
if exitCode != 0 {
|
||||
@@ -50,7 +51,8 @@ func TestCmd_Run(t *testing.T) {
|
||||
})
|
||||
|
||||
cmd := Cmd{
|
||||
Login: login,
|
||||
Login: login,
|
||||
Logger: t,
|
||||
}
|
||||
exitCode := cmd.Run(ctx, []string{executable,
|
||||
"--listen-port", "10080",
|
||||
@@ -66,7 +68,8 @@ func TestCmd_Run(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
cmd := Cmd{
|
||||
Login: mock_usecases.NewMockLogin(ctrl),
|
||||
Login: mock_usecases.NewMockLogin(ctrl),
|
||||
Logger: t,
|
||||
}
|
||||
exitCode := cmd.Run(context.TODO(), []string{executable, "some"}, version)
|
||||
if exitCode != 1 {
|
||||
|
||||
@@ -52,3 +52,7 @@ type OIDCAuthenticateOut struct {
|
||||
IDToken string
|
||||
RefreshToken string
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
Logf(format string, v ...interface{})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package adaptors
|
||||
|
||||
import "log"
|
||||
|
||||
type Logger struct{}
|
||||
|
||||
func (*Logger) Logf(format string, v ...interface{}) {
|
||||
log.Printf(format, v...)
|
||||
}
|
||||
@@ -14,6 +14,7 @@ func Invoke(f func(cmd adaptorsInterfaces.Cmd)) error {
|
||||
KubeConfig: &adaptors.KubeConfig{},
|
||||
HTTP: &adaptors.HTTP{},
|
||||
OIDC: &adaptors.OIDC{},
|
||||
Logger: &adaptors.Logger{},
|
||||
},
|
||||
})
|
||||
return nil
|
||||
|
||||
+9
-9
@@ -2,7 +2,6 @@ package usecases
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/int128/kubelogin/adaptors/interfaces"
|
||||
"github.com/int128/kubelogin/kubeconfig"
|
||||
@@ -21,6 +20,7 @@ type Login struct {
|
||||
KubeConfig adaptors.KubeConfig
|
||||
HTTP adaptors.HTTP
|
||||
OIDC adaptors.OIDC
|
||||
Logger adaptors.Logger
|
||||
}
|
||||
|
||||
func (u *Login) Do(ctx context.Context, in usecases.LoginIn) error {
|
||||
@@ -29,10 +29,10 @@ func (u *Login) Do(ctx context.Context, in usecases.LoginIn) error {
|
||||
return errors.Wrapf(err, "could not read the kubeconfig")
|
||||
}
|
||||
|
||||
log.Printf("Using current-context: %s", cfg.CurrentContext)
|
||||
u.Logger.Logf("Using current-context: %s", cfg.CurrentContext)
|
||||
authProvider, err := kubeconfig.FindOIDCAuthProvider(cfg)
|
||||
if err != nil {
|
||||
log.Printf(oidcConfigErrorMessage, cfg.CurrentContext)
|
||||
u.Logger.Logf(oidcConfigErrorMessage, cfg.CurrentContext)
|
||||
return errors.Wrapf(err, "could not find an oidc auth-provider in the kubeconfig")
|
||||
}
|
||||
|
||||
@@ -40,16 +40,16 @@ func (u *Login) Do(ctx context.Context, in usecases.LoginIn) error {
|
||||
clientConfig.SetSkipTLSVerify(in.SkipTLSVerify)
|
||||
if authProvider.IDPCertificateAuthority() != "" {
|
||||
filename := authProvider.IDPCertificateAuthority()
|
||||
log.Printf("Using the certificate %s", filename)
|
||||
u.Logger.Logf("Using the certificate %s", filename)
|
||||
if err := clientConfig.AddCertificateFromFile(filename); err != nil {
|
||||
log.Printf("Skip the certificate %s: %s", filename, err)
|
||||
u.Logger.Logf("Skip the certificate %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
if authProvider.IDPCertificateAuthorityData() != "" {
|
||||
encoded := authProvider.IDPCertificateAuthorityData()
|
||||
log.Printf("Using certificate of idp-certificate-authority-data")
|
||||
u.Logger.Logf("Using certificate of idp-certificate-authority-data")
|
||||
if err := clientConfig.AddEncodedCertificate(encoded); err != nil {
|
||||
log.Printf("Skip the certificate of idp-certificate-authority-data: %s", err)
|
||||
u.Logger.Logf("Skip the certificate of idp-certificate-authority-data: %s", err)
|
||||
}
|
||||
}
|
||||
hc, err := u.HTTP.NewClient(clientConfig)
|
||||
@@ -70,12 +70,12 @@ func (u *Login) Do(ctx context.Context, in usecases.LoginIn) error {
|
||||
return errors.Wrapf(err, "could not get token from OIDC provider")
|
||||
}
|
||||
|
||||
log.Printf("Got a token for subject=%s", out.VerifiedIDToken.Subject)
|
||||
u.Logger.Logf("Got a token for subject=%s", out.VerifiedIDToken.Subject)
|
||||
authProvider.SetIDToken(out.IDToken)
|
||||
authProvider.SetRefreshToken(out.RefreshToken)
|
||||
if err := u.KubeConfig.WriteToFile(cfg, in.KubeConfig); err != nil {
|
||||
return errors.Wrapf(err, "could not update the kubeconfig")
|
||||
}
|
||||
log.Printf("Updated %s", in.KubeConfig)
|
||||
u.Logger.Logf("Updated %s", in.KubeConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -102,6 +102,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -143,6 +144,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -186,6 +188,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -229,6 +232,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -273,6 +277,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -318,6 +323,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -362,6 +368,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
@@ -407,6 +414,7 @@ func TestLogin_Do(t *testing.T) {
|
||||
KubeConfig: newMockKubeConfig(ctrl, inConfig, outConfig),
|
||||
HTTP: newMockHTTP(ctrl, httpClientConfig),
|
||||
OIDC: mockOIDC,
|
||||
Logger: t,
|
||||
}
|
||||
if err := u.Do(ctx, usecases.LoginIn{
|
||||
KubeConfig: "/path/to/kubeconfig",
|
||||
|
||||
Reference in New Issue
Block a user