Skip to content

Commit bc57c1c

Browse files
cstocktonChris Stockton
and
Chris Stockton
authored
feat: increase test coverage in conf package to 100% (#1937)
Increase test coverage in internal/conf to 100%. --------- Co-authored-by: Chris Stockton <[email protected]>
1 parent e137365 commit bc57c1c

File tree

9 files changed

+1550
-89
lines changed

9 files changed

+1550
-89
lines changed

internal/conf/configuration.go

+52-31
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,15 @@ func LoadDirectory(configDir string) error {
767767
// If at least one path was found we load the configuration files in the
768768
// directory. We don't call override without config files because it will
769769
// override the env vars previously set with a ".env", if one exists.
770-
if len(paths) > 0 {
771-
if err := godotenv.Overload(paths...); err != nil {
770+
return loadDirectoryPaths(paths...)
771+
}
772+
773+
func loadDirectoryPaths(p ...string) error {
774+
// If at least one path was found we load the configuration files in the
775+
// directory. We don't call override without config files because it will
776+
// override the env vars previously set with a ".env", if one exists.
777+
if len(p) > 0 {
778+
if err := godotenv.Overload(p...); err != nil {
772779
return err
773780
}
774781
}
@@ -811,7 +818,10 @@ func loadGlobal(config *GlobalConfiguration) error {
811818
if err := config.Validate(); err != nil {
812819
return err
813820
}
821+
return populateGlobal(config)
822+
}
814823

824+
func populateGlobal(config *GlobalConfiguration) error {
815825
if config.Hook.PasswordVerificationAttempt.Enabled {
816826
if err := config.Hook.PasswordVerificationAttempt.PopulateExtensibilityPoint(); err != nil {
817827
return err
@@ -892,37 +902,9 @@ func (config *GlobalConfiguration) ApplyDefaults() error {
892902

893903
if len(config.JWT.Keys) == 0 {
894904
// transform the secret into a JWK for consistency
895-
privKey, err := jwk.FromRaw([]byte(config.JWT.Secret))
896-
if err != nil {
905+
if err := config.applyDefaultsJWT([]byte(config.JWT.Secret)); err != nil {
897906
return err
898907
}
899-
if config.JWT.KeyID != "" {
900-
if err := privKey.Set(jwk.KeyIDKey, config.JWT.KeyID); err != nil {
901-
return err
902-
}
903-
}
904-
if privKey.Algorithm().String() == "" {
905-
if err := privKey.Set(jwk.AlgorithmKey, jwt.SigningMethodHS256.Name); err != nil {
906-
return err
907-
}
908-
}
909-
if err := privKey.Set(jwk.KeyUsageKey, "sig"); err != nil {
910-
return err
911-
}
912-
if len(privKey.KeyOps()) == 0 {
913-
if err := privKey.Set(jwk.KeyOpsKey, jwk.KeyOperationList{jwk.KeyOpSign, jwk.KeyOpVerify}); err != nil {
914-
return err
915-
}
916-
}
917-
pubKey, err := privKey.PublicKey()
918-
if err != nil {
919-
return err
920-
}
921-
config.JWT.Keys = make(JwtKeysDecoder)
922-
config.JWT.Keys[config.JWT.KeyID] = JwkInfo{
923-
PublicKey: pubKey,
924-
PrivateKey: privKey,
925-
}
926908
}
927909

928910
if config.JWT.ValidMethods == nil {
@@ -1036,6 +1018,45 @@ func (config *GlobalConfiguration) ApplyDefaults() error {
10361018

10371019
return nil
10381020
}
1021+
func (config *GlobalConfiguration) applyDefaultsJWT(secret []byte) error {
1022+
// transform the secret into a JWK for consistency
1023+
privKey, err := jwk.FromRaw(secret)
1024+
if err != nil {
1025+
return err
1026+
}
1027+
return config.applyDefaultsJWTPrivateKey(privKey)
1028+
}
1029+
1030+
func (config *GlobalConfiguration) applyDefaultsJWTPrivateKey(privKey jwk.Key) error {
1031+
if config.JWT.KeyID != "" {
1032+
if err := privKey.Set(jwk.KeyIDKey, config.JWT.KeyID); err != nil {
1033+
return err
1034+
}
1035+
}
1036+
if privKey.Algorithm().String() == "" {
1037+
if err := privKey.Set(jwk.AlgorithmKey, jwt.SigningMethodHS256.Name); err != nil {
1038+
return err
1039+
}
1040+
}
1041+
if err := privKey.Set(jwk.KeyUsageKey, "sig"); err != nil {
1042+
return err
1043+
}
1044+
if len(privKey.KeyOps()) == 0 {
1045+
if err := privKey.Set(jwk.KeyOpsKey, jwk.KeyOperationList{jwk.KeyOpSign, jwk.KeyOpVerify}); err != nil {
1046+
return err
1047+
}
1048+
}
1049+
pubKey, err := privKey.PublicKey()
1050+
if err != nil {
1051+
return err
1052+
}
1053+
config.JWT.Keys = make(JwtKeysDecoder)
1054+
config.JWT.Keys[config.JWT.KeyID] = JwkInfo{
1055+
PublicKey: pubKey,
1056+
PrivateKey: privKey,
1057+
}
1058+
return nil
1059+
}
10391060

10401061
// Validate validates all of configuration.
10411062
func (c *GlobalConfiguration) Validate() error {

0 commit comments

Comments
 (0)