-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor authn-k8s-client to be authentication flow generic
- Loading branch information
1 parent
b28a639
commit c5ec2e1
Showing
22 changed files
with
578 additions
and
554 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package authenticator | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/access_token" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/common" | ||
k8sAuthenitcator "github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/k8s" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/log" | ||
) | ||
|
||
func NewAuthenticator(conf common.ConfigurationInterface) (common.AuthenticatorInterface, error) { | ||
authn, error := getAuthenticator(conf) | ||
if error != nil { | ||
return nil, error | ||
} | ||
return authn.Init(&conf) | ||
} | ||
|
||
func NewAuthenticatorWithAccessToken(conf common.ConfigurationInterface, token access_token.AccessToken) (common.AuthenticatorInterface, error) { | ||
var error error | ||
var authn common.AuthenticatorInterface | ||
authn, error = getAuthenticator(conf) | ||
if error != nil { | ||
return nil, error | ||
} | ||
return authn.InitWithAccessToken(&conf, token) | ||
} | ||
|
||
func getAuthenticator(conf common.ConfigurationInterface) (common.AuthenticatorInterface, error) { | ||
if conf.GetAuthenticationType() == k8sAuthenitcator.AuthnType { | ||
return &k8sAuthenitcator.Authenticator{}, nil | ||
} | ||
return nil, fmt.Errorf(log.CAKC064) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/access_token" | ||
) | ||
|
||
type AuthenticatorInterface interface { | ||
Init(config *ConfigurationInterface) (AuthenticatorInterface, error) | ||
InitWithAccessToken(config *ConfigurationInterface, token access_token.AccessToken) (AuthenticatorInterface, error) | ||
Authenticate() error | ||
GetAccessToken() access_token.AccessToken | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/log" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// Config defines the configuration parameters | ||
// for the authentication requests | ||
type Config struct { | ||
Account string | ||
ClientCertPath string | ||
ClientCertRetryCountLimit int | ||
ContainerMode string | ||
SSLCertificate []byte | ||
TokenFilePath string | ||
TokenRefreshTimeout time.Duration | ||
URL string | ||
Username *Username | ||
} | ||
|
||
func (config *Config) LoadConfig(settings map[string]string) { | ||
for key, value := range settings { | ||
switch key { | ||
case "CONJUR_ACCOUNT": | ||
config.Account = value | ||
case "CONJUR_AUTHN_LOGIN": | ||
username, _ := NewUsername(value) | ||
config.Username = username | ||
case "CONJUR_AUTHN_URL": | ||
config.URL = value | ||
case "CONJUR_SSL_CERTIFICATE": | ||
config.SSLCertificate = []byte(value) | ||
case "CONTAINER_MODE": | ||
config.ContainerMode = value | ||
case "CONJUR_AUTHN_TOKEN_FILE": | ||
config.TokenFilePath = value | ||
case "CONJUR_CLIENT_CERT_PATH": | ||
config.ClientCertPath = value | ||
case "CONJUR_CLIENT_CERT_RETRY_COUNT_LIMIT": | ||
limit, _ := strconv.Atoi(value) | ||
config.ClientCertRetryCountLimit = limit | ||
case "CONJUR_TOKEN_TIMEOUT": | ||
timeout, _ := durationFromString(key, value) | ||
config.TokenRefreshTimeout = timeout | ||
} | ||
} | ||
} | ||
|
||
func durationFromString(key, value string) (time.Duration, error) { | ||
duration, err := time.ParseDuration(value) | ||
if err != nil { | ||
return 0, fmt.Errorf(log.CAKC060, key, value) | ||
} | ||
return duration, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package common | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ConfigurationInterface interface { | ||
LoadConfig(settings map[string]string) | ||
GetAuthenticationType() string | ||
GetEnvVariables() []string | ||
GetRequiredVariables() []string | ||
GetDefaultValues() map[string]string | ||
GetContainerMode() string | ||
GetTokenFilePath() string | ||
GetTokenTimeout() time.Duration | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/authenticator/config/username.go → pkg/authenticator/common/username.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package config | ||
package common | ||
|
||
import ( | ||
"strings" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cyberark/conjur-authn-k8s-client/pkg/log" | ||
) | ||
|
||
// ReadFileFunc defines the interface for reading an SSL Certificate from the env | ||
type ReadFileFunc func(filename string) ([]byte, error) | ||
|
||
func validTimeout(key, timeoutStr string) error { | ||
_, err := durationFromString(key, timeoutStr) | ||
return err | ||
} | ||
|
||
func validInt(key, value string) error { | ||
_, err := strconv.Atoi(value) | ||
if err != nil { | ||
return fmt.Errorf(log.CAKC060, key, value) | ||
} | ||
return nil | ||
} | ||
|
||
func validUsername(key, value string) error { | ||
_, err := NewUsername(value) | ||
return err | ||
} | ||
|
||
func validConjurVersion(key, version string) error { | ||
// Only versions '4' & '5' are allowed, with '5' being used as the default | ||
switch version { | ||
case "4": | ||
break | ||
case "5": | ||
break | ||
default: | ||
return fmt.Errorf(log.CAKC060, key, version) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateSetting(key string, value string) error { | ||
switch key { | ||
case "CONJUR_AUTHN_LOGIN": | ||
return validUsername(key, value) | ||
case "CONJUR_CLIENT_CERT_RETRY_COUNT_LIMIT": | ||
return validInt(key, value) | ||
case "CONJUR_TOKEN_TIMEOUT": | ||
return validTimeout(key, value) | ||
case "CONJUR_VERSION": | ||
return validConjurVersion(key, value) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func ReadSSLCert(settings map[string]string, readFile ReadFileFunc) ([]byte, error) { | ||
SSLCert := settings["CONJUR_SSL_CERTIFICATE"] | ||
SSLCertPath := settings["CONJUR_CERT_FILE"] | ||
if SSLCert == "" && SSLCertPath == "" { | ||
return nil, errors.New(log.CAKC007) | ||
} | ||
|
||
if SSLCert != "" { | ||
return []byte(SSLCert), nil | ||
} | ||
return readFile(SSLCertPath) | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/authenticator/version.go → pkg/authenticator/common/version.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package authenticator | ||
package common | ||
|
||
import "fmt" | ||
|
||
|
Oops, something went wrong.