-
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
89545e7
commit 6745595
Showing
23 changed files
with
867 additions
and
872 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,30 @@ | ||
package authenticator | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/access_token" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/access_token/file" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/config" | ||
k8sAuthenitcator "github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/k8s" | ||
"github.com/cyberark/conjur-authn-k8s-client/pkg/log" | ||
) | ||
|
||
func NewAuthenticator(conf config.ConfigurationInterface) (AuthenticatorInterface, error) { | ||
accessToken, error := file.NewAccessToken(conf.GetTokenFilePath()) | ||
if error != nil { | ||
return nil, error | ||
} | ||
return getAuthenticator(conf, accessToken) | ||
} | ||
|
||
func NewAuthenticatorWithAccessToken(conf config.ConfigurationInterface, token access_token.AccessToken) (AuthenticatorInterface, error) { | ||
return getAuthenticator(conf, token) | ||
} | ||
|
||
func getAuthenticator(conf config.ConfigurationInterface, token access_token.AccessToken) (AuthenticatorInterface, error) { | ||
if conf.GetAuthenticationType() == k8sAuthenitcator.AuthnType { | ||
k8sCfg := (conf).(*k8sAuthenitcator.Config) | ||
return k8sAuthenitcator.NewWithAccessToken(*k8sCfg, token) | ||
} | ||
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,10 @@ | ||
package authenticator | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type AuthenticatorInterface interface { | ||
Authenticate() error | ||
AuthenticateWithContext(ctx context.Context) error | ||
} |
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 | ||
} |
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) | ||
} |
Oops, something went wrong.