2022-04-08 11:40:59 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-08-06 12:50:20 -07:00
package authmethods_test
import (
2023-10-11 20:45:50 -04:00
stdcmp "cmp"
2020-10-12 18:03:15 -04:00
"context"
2023-03-15 11:40:40 -04:00
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
2020-08-06 12:50:20 -07:00
"fmt"
2021-04-08 20:54:32 -04:00
"regexp"
2023-10-11 20:45:50 -04:00
"slices"
2020-08-06 12:50:20 -07:00
"strings"
"testing"
"github.com/google/go-cmp/cmp"
2023-10-31 13:44:17 +00:00
"github.com/google/go-cmp/cmp/cmpopts"
2022-10-28 15:52:06 -04:00
"github.com/hashicorp/boundary/globals"
2023-03-15 11:40:40 -04:00
"github.com/hashicorp/boundary/internal/auth/ldap"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/auth/oidc"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/auth/password"
2020-10-12 18:03:15 -04:00
"github.com/hashicorp/boundary/internal/authtoken"
2022-05-09 12:45:10 -07:00
requestauth "github.com/hashicorp/boundary/internal/daemon/controller/auth"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/authmethods"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-10 17:15:02 -04:00
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
2020-08-19 16:47:18 -04:00
"github.com/hashicorp/boundary/internal/kms"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/types/action"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/types/scope"
2021-08-26 10:44:41 -04:00
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/authmethods"
scopepb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/scopes"
2020-08-06 12:50:20 -07:00
"google.golang.org/grpc/codes"
2023-03-15 11:40:40 -04:00
"google.golang.org/protobuf/proto"
2020-08-06 12:50:20 -07:00
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/structpb"
2021-03-24 16:07:43 -04:00
"google.golang.org/protobuf/types/known/timestamppb"
2020-08-06 12:50:20 -07:00
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2020-10-12 18:03:15 -04:00
const (
testPassword = "thetestpassword"
testLoginName = "default"
)
2021-04-08 20:54:32 -04:00
var (
pwAuthorizedActions = [] string {
2021-04-23 09:13:37 -05:00
action . NoOp . String (),
2021-04-08 20:54:32 -04:00
action . Read . String (),
action . Update . String (),
action . Delete . String (),
action . Authenticate . String (),
}
oidcAuthorizedActions = [] string {
2021-04-23 09:13:37 -05:00
action . NoOp . String (),
2021-04-08 20:54:32 -04:00
action . Read . String (),
action . Update . String (),
action . Delete . String (),
action . ChangeState . String (),
action . Authenticate . String (),
}
2023-03-15 11:40:40 -04:00
ldapAuthorizedActions = [] string {
action . NoOp . String (),
action . Read . String (),
action . Update . String (),
action . Delete . String (),
action . Authenticate . String (),
}
2021-04-08 20:54:32 -04:00
)
2021-01-20 10:53:01 -05:00
var authorizedCollectionActions = map [ string ] * structpb . ListValue {
"accounts" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
2021-11-12 09:28:07 -05:00
"managed-groups" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
2021-01-20 10:53:01 -05:00
}
2020-08-06 12:50:20 -07:00
func TestGet ( t * testing . T ) {
2021-07-29 18:52:56 -06:00
ctx := context . TODO ()
2020-08-06 12:50:20 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
2020-08-19 16:47:18 -04:00
wrapper := db . TestWrapper ( t )
2021-04-08 20:54:32 -04:00
kmsCache := kms . TestKms ( t , conn , wrapper )
2020-09-04 14:48:11 -07:00
iamRepoFn := func () ( * iam . Repository , error ) {
return iam . TestRepo ( t , conn , wrapper ), nil
}
2021-04-08 20:54:32 -04:00
oidcRepoFn := func () ( * oidc . Repository , error ) {
2021-07-29 18:52:56 -06:00
return oidc . NewRepository ( ctx , rw , rw , kmsCache )
2021-04-08 20:54:32 -04:00
}
2023-03-15 11:40:40 -04:00
ldapRepoFn := func () ( * ldap . Repository , error ) {
return ldap . NewRepository ( ctx , rw , rw , kmsCache )
}
2020-10-12 18:03:15 -04:00
pwRepoFn := func () ( * password . Repository , error ) {
2023-06-06 17:09:08 -04:00
return password . NewRepository ( ctx , rw , rw , kmsCache )
2020-08-06 12:50:20 -07:00
}
2020-10-12 18:03:15 -04:00
atRepoFn := func () ( * authtoken . Repository , error ) {
2023-06-06 17:09:08 -04:00
return authtoken . NewRepository ( ctx , rw , rw , kmsCache )
2020-10-12 18:03:15 -04:00
}
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
2020-08-06 12:50:20 -07:00
2020-08-19 16:47:18 -04:00
o , _ := iam . TestScopes ( t , iamRepo )
2020-08-06 12:50:20 -07:00
am := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]
2021-04-08 20:54:32 -04:00
iam . TestSetPrimaryAuthMethod ( t , iamRepo , o , am . GetPublicId ())
2020-08-06 12:50:20 -07:00
2021-04-08 20:54:32 -04:00
wantPw := & pb . AuthMethod {
2020-08-06 12:50:20 -07:00
Id : am . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : am . GetScopeId (),
2020-08-06 12:50:20 -07:00
CreatedTime : am . CreateTime . GetTimestamp (),
UpdatedTime : am . UpdateTime . GetTimestamp (),
Type : "password" ,
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_PasswordAuthMethodAttributes {
PasswordAuthMethodAttributes : & pb . PasswordAuthMethodAttributes {
MinPasswordLength : 8 ,
MinLoginNameLength : 3 ,
},
},
2020-08-11 11:19:29 -04:00
Version : 1 ,
2020-08-12 08:20:35 -07:00
Scope : & scopepb . ScopeInfo {
2021-03-25 03:15:43 -04:00
Id : o . GetPublicId (),
Type : o . GetType (),
ParentScopeId : scope . Global . String (),
2020-08-06 12:50:20 -07:00
},
2021-04-08 20:54:32 -04:00
IsPrimary : true ,
AuthorizedActions : pwAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
}
databaseWrapper , err := kmsCache . GetWrapper ( context . Background (), o . GetPublicId (), kms . KeyPurposeDatabase )
require . NoError ( t , err )
oidcam := oidc . TestAuthMethod ( t , conn , databaseWrapper , o . GetPublicId (), oidc . InactiveState , "alice_rp" , "secret" ,
2023-11-21 11:30:02 -05:00
oidc . WithIssuer ( oidc . TestConvertToUrls ( t , "https://alice.com" )[ 0 ]), oidc . WithApiUrl ( oidc . TestConvertToUrls ( t , "https://api.com" )[ 0 ]),
oidc . WithPrompts ( oidc . SelectAccount ))
2021-04-08 20:54:32 -04:00
wantOidc := & pb . AuthMethod {
Id : oidcam . GetPublicId (),
ScopeId : oidcam . GetScopeId (),
CreatedTime : oidcam . CreateTime . GetTimestamp (),
UpdatedTime : oidcam . UpdateTime . GetTimestamp (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://alice.com" ),
ClientId : wrapperspb . String ( "alice_rp" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . InactiveState ),
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
CallbackUrl : fmt . Sprintf ( oidc . CallbackEndpoint , "https://api.com" ),
2023-11-21 11:30:02 -05:00
Prompts : [] string { string ( oidc . SelectAccount )},
2022-04-08 11:40:59 -04:00
},
},
2021-04-08 20:54:32 -04:00
Version : 1 ,
Scope : & scopepb . ScopeInfo {
Id : o . GetPublicId (),
Type : o . GetType (),
ParentScopeId : scope . Global . String (),
},
AuthorizedActions : oidcAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-08-06 12:50:20 -07:00
}
2023-09-06 15:07:45 -04:00
ldapAm := ldap . TestAuthMethod ( t , conn , databaseWrapper , o . GetPublicId (), [] string { "ldaps://ldap1" },
ldap . WithAccountAttributeMap ( ctx , map [ string ] ldap . AccountToAttribute {
"mail" : ldap . ToEmailAttribute ,
}),
ldap . WithMaximumPageSize ( ctx , 10 ),
ldap . WithDerefAliases ( ctx , ldap . DerefAlways ),
)
2023-03-15 11:40:40 -04:00
wantLdap := & pb . AuthMethod {
Id : ldapAm . GetPublicId (),
ScopeId : ldapAm . GetScopeId (),
CreatedTime : ldapAm . CreateTime . GetTimestamp (),
UpdatedTime : ldapAm . UpdateTime . GetTimestamp (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
State : string ( ldap . InactiveState ),
Urls : [] string { "ldaps://ldap1" },
AccountAttributeMaps : [] string { "mail=email" },
2023-09-06 15:07:45 -04:00
MaximumPageSize : 10 ,
DereferenceAliases : wrapperspb . String ( string ( ldap . DerefAlways )),
2023-03-15 11:40:40 -04:00
},
},
Version : 1 ,
Scope : & scopepb . ScopeInfo {
Id : o . GetPublicId (),
Type : o . GetType (),
ParentScopeId : scope . Global . String (),
},
AuthorizedActions : ldapAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
}
2020-08-06 12:50:20 -07:00
cases := [] struct {
name string
scopeId string
req * pbs . GetAuthMethodRequest
res * pbs . GetAuthMethodResponse
2020-09-23 08:10:27 -07:00
err error
2020-08-06 12:50:20 -07:00
}{
{
2021-04-08 20:54:32 -04:00
name : "Get an Existing PW AuthMethod" ,
2020-08-06 12:50:20 -07:00
scopeId : o . GetPublicId (),
req : & pbs . GetAuthMethodRequest { Id : am . GetPublicId ()},
2021-04-08 20:54:32 -04:00
res : & pbs . GetAuthMethodResponse { Item : wantPw },
},
{
name : "Get an Existing OIDC AuthMethod" ,
scopeId : o . GetPublicId (),
req : & pbs . GetAuthMethodRequest { Id : oidcam . GetPublicId ()},
res : & pbs . GetAuthMethodResponse { Item : wantOidc },
2020-08-06 12:50:20 -07:00
},
{
2023-03-15 11:40:40 -04:00
name : "Get an Existing LDAP AuthMethod" ,
scopeId : o . GetPublicId (),
req : & pbs . GetAuthMethodRequest { Id : ldapAm . GetPublicId ()},
res : & pbs . GetAuthMethodResponse { Item : wantLdap },
},
{
name : "Get a non existent AuthMethod" ,
2020-08-06 12:50:20 -07:00
scopeId : o . GetPublicId (),
2023-03-10 15:06:39 -05:00
req : & pbs . GetAuthMethodRequest { Id : globals . PasswordAuthMethodPrefix + "_DoesntExis" },
2020-08-06 12:50:20 -07:00
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-08-06 12:50:20 -07:00
},
{
name : "Wrong id prefix" ,
scopeId : o . GetPublicId (),
req : & pbs . GetAuthMethodRequest { Id : "j_1234567890" },
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
name : "space in id" ,
scopeId : o . GetPublicId (),
2023-03-10 15:06:39 -05:00
req : & pbs . GetAuthMethodRequest { Id : globals . PasswordAuthMethodPrefix + "_1 23456789" },
2020-08-06 12:50:20 -07:00
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
2023-06-06 17:09:08 -04:00
s , err := authmethods . NewService ( ctx , kmsCache , pwRepoFn , oidcRepoFn , iamRepoFn , atRepoFn , ldapRepoFn )
2020-08-06 12:50:20 -07:00
require . NoError ( err , "Couldn't create new auth_method service." )
2021-07-27 09:59:52 -07:00
got , gErr := s . GetAuthMethod ( requestauth . DisabledAuthTestContext ( iamRepoFn , tc . scopeId ), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "GetAuthMethod(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
2021-04-08 20:54:32 -04:00
return
}
require . NoError ( gErr )
2022-04-08 11:40:59 -04:00
if oidcAttrs := got . Item . GetOidcAuthMethodsAttributes (); oidcAttrs != nil {
assert . NotEqual ( "secret" , oidcAttrs . ClientSecretHmac )
2020-09-23 08:10:27 -07:00
}
2023-10-31 13:44:17 +00:00
assert . Empty ( cmp . Diff (
got ,
tc . res ,
protocmp . Transform (),
protocmp . IgnoreFields ( & pb . OidcAuthMethodAttributes {}, "client_secret_hmac" ),
cmpopts . SortSlices ( func ( a , b string ) bool {
return a < b
}),
cmpopts . SortSlices ( func ( a , b protocmp . Message ) bool {
return a . String () < b . String ()
}),
), "GetAuthMethod(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-08-06 12:50:20 -07:00
})
}
}
func TestList ( t * testing . T ) {
2021-07-29 18:52:56 -06:00
ctx := context . TODO ()
2020-08-06 12:50:20 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
2020-08-19 16:47:18 -04:00
wrapper := db . TestWrapper ( t )
2021-04-08 20:54:32 -04:00
kmsCache := kms . TestKms ( t , conn , wrapper )
2020-09-04 14:48:11 -07:00
iamRepoFn := func () ( * iam . Repository , error ) {
return iam . TestRepo ( t , conn , wrapper ), nil
}
2021-04-08 20:54:32 -04:00
oidcRepoFn := func () ( * oidc . Repository , error ) {
2021-07-29 18:52:56 -06:00
return oidc . NewRepository ( ctx , rw , rw , kmsCache )
2021-04-08 20:54:32 -04:00
}
2023-03-15 11:40:40 -04:00
ldapRepoFn := func () ( * ldap . Repository , error ) {
return ldap . NewRepository ( ctx , rw , rw , kmsCache )
}
2020-10-12 18:03:15 -04:00
pwRepoFn := func () ( * password . Repository , error ) {
2023-06-06 17:09:08 -04:00
return password . NewRepository ( ctx , rw , rw , kmsCache )
2020-08-06 12:50:20 -07:00
}
2020-10-12 18:03:15 -04:00
atRepoFn := func () ( * authtoken . Repository , error ) {
2023-06-06 17:09:08 -04:00
return authtoken . NewRepository ( ctx , rw , rw , kmsCache )
2020-10-12 18:03:15 -04:00
}
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
2020-08-06 12:50:20 -07:00
2020-08-19 16:47:18 -04:00
oNoAuthMethods , _ := iam . TestScopes ( t , iamRepo )
oWithAuthMethods , _ := iam . TestScopes ( t , iamRepo )
oWithOtherAuthMethods , _ := iam . TestScopes ( t , iamRepo )
2020-08-06 12:50:20 -07:00
var wantSomeAuthMethods [] * pb . AuthMethod
2021-04-08 20:54:32 -04:00
databaseWrapper , err := kmsCache . GetWrapper ( context . Background (), oWithAuthMethods . GetPublicId (), kms . KeyPurposeDatabase )
require . NoError ( t , err )
2021-04-13 16:42:25 -07:00
oidcam := oidc . TestAuthMethod ( t , conn , databaseWrapper , oWithAuthMethods . GetPublicId (), oidc . ActivePublicState , "alice_rp" , "secret" ,
2023-11-21 11:30:02 -05:00
oidc . WithIssuer ( oidc . TestConvertToUrls ( t , "https://alice.com" )[ 0 ]), oidc . WithApiUrl ( oidc . TestConvertToUrls ( t , "https://api.com" )[ 0 ]), oidc . WithSigningAlgs ( oidc . EdDSA ),
oidc . WithPrompts ( oidc . Consent ))
2021-04-08 20:54:32 -04:00
iam . TestSetPrimaryAuthMethod ( t , iamRepo , oWithAuthMethods , oidcam . GetPublicId ())
wantSomeAuthMethods = append ( wantSomeAuthMethods , & pb . AuthMethod {
Id : oidcam . GetPublicId (),
ScopeId : oWithAuthMethods . GetPublicId (),
CreatedTime : oidcam . GetCreateTime (). GetTimestamp (),
UpdatedTime : oidcam . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : oWithAuthMethods . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String ()},
2021-04-13 16:42:25 -07:00
Version : 2 ,
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://alice.com" ),
ClientId : wrapperspb . String ( "alice_rp" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . ActivePublicState ),
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
CallbackUrl : fmt . Sprintf ( oidc . CallbackEndpoint , "https://api.com" ),
SigningAlgorithms : [] string {
string ( oidc . EdDSA ),
},
2023-11-21 11:30:02 -05:00
Prompts : [] string { string ( oidc . Consent )},
2022-04-08 11:40:59 -04:00
},
},
2021-04-08 20:54:32 -04:00
IsPrimary : true ,
AuthorizedActions : oidcAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
})
2020-08-06 12:50:20 -07:00
for _ , am := range password . TestAuthMethods ( t , conn , oWithAuthMethods . GetPublicId (), 3 ) {
wantSomeAuthMethods = append ( wantSomeAuthMethods , & pb . AuthMethod {
Id : am . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : oWithAuthMethods . GetPublicId (),
2020-08-06 12:50:20 -07:00
CreatedTime : am . GetCreateTime (). GetTimestamp (),
UpdatedTime : am . GetUpdateTime (). GetTimestamp (),
2021-03-25 03:15:43 -04:00
Scope : & scopepb . ScopeInfo { Id : oWithAuthMethods . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String ()},
2020-08-11 11:19:29 -04:00
Version : 1 ,
2020-08-06 12:50:20 -07:00
Type : "password" ,
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_PasswordAuthMethodAttributes {
PasswordAuthMethodAttributes : & pb . PasswordAuthMethodAttributes {
MinPasswordLength : 8 ,
MinLoginNameLength : 3 ,
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : pwAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-08-06 12:50:20 -07:00
})
}
2023-10-11 20:45:50 -04:00
sorterFn := func ( a * pb . AuthMethod , b * pb . AuthMethod ) int {
return stdcmp . Compare ( a . GetId (), b . GetId ())
2023-03-15 11:40:40 -04:00
}
cpSorted := func ( ams [] * pb . AuthMethod ) [] * pb . AuthMethod {
cp := make ([] * pb . AuthMethod , 0 , len ( ams ))
for _ , a := range ams {
cp = append ( cp , proto . Clone ( a ).( * pb . AuthMethod ))
}
slices . SortFunc ( cp , sorterFn )
return cp
}
2023-09-06 15:07:45 -04:00
ldapAm := ldap . TestAuthMethod ( t , conn , databaseWrapper , oWithAuthMethods . GetPublicId (), [] string { "ldaps://ldap1" },
ldap . WithOperationalState ( ctx , ldap . ActivePublicState ),
ldap . WithMaximumPageSize ( ctx , 10 ),
ldap . WithDerefAliases ( ctx , ldap . DerefAlways ),
)
2023-03-15 11:40:40 -04:00
wantSomeAuthMethods = append ( wantSomeAuthMethods , & pb . AuthMethod {
Id : ldapAm . GetPublicId (),
ScopeId : oWithAuthMethods . GetPublicId (),
CreatedTime : ldapAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : ldapAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : oWithAuthMethods . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String ()},
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
2023-09-06 15:07:45 -04:00
State : string ( ldap . ActivePublicState ),
Urls : [] string { "ldaps://ldap1" },
MaximumPageSize : 10 ,
DereferenceAliases : wrapperspb . String ( string ( ldap . DerefAlways )),
2023-03-15 11:40:40 -04:00
},
},
AuthorizedActions : ldapAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
})
2020-08-06 12:50:20 -07:00
var wantOtherAuthMethods [] * pb . AuthMethod
for _ , aa := range password . TestAuthMethods ( t , conn , oWithOtherAuthMethods . GetPublicId (), 3 ) {
wantOtherAuthMethods = append ( wantOtherAuthMethods , & pb . AuthMethod {
Id : aa . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : oWithOtherAuthMethods . GetPublicId (),
2020-08-06 12:50:20 -07:00
CreatedTime : aa . GetCreateTime (). GetTimestamp (),
UpdatedTime : aa . GetUpdateTime (). GetTimestamp (),
2021-03-25 03:15:43 -04:00
Scope : & scopepb . ScopeInfo { Id : oWithOtherAuthMethods . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String ()},
2020-08-11 11:19:29 -04:00
Version : 1 ,
2020-08-06 12:50:20 -07:00
Type : "password" ,
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_PasswordAuthMethodAttributes {
PasswordAuthMethodAttributes : & pb . PasswordAuthMethodAttributes {
MinPasswordLength : 8 ,
MinLoginNameLength : 3 ,
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : pwAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-08-06 12:50:20 -07:00
})
}
cases := [] struct {
2021-01-25 19:37:40 -05:00
name string
req * pbs . ListAuthMethodsRequest
res * pbs . ListAuthMethodsResponse
err error
2020-08-06 12:50:20 -07:00
}{
{
2021-01-25 19:37:40 -05:00
name : "List Some Auth Methods" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : oWithAuthMethods . GetPublicId ()},
2023-03-15 11:40:40 -04:00
res : & pbs . ListAuthMethodsResponse { Items : cpSorted ( wantSomeAuthMethods )},
2020-08-06 12:50:20 -07:00
},
{
2021-01-25 19:37:40 -05:00
name : "List Other Auth Methods" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : oWithOtherAuthMethods . GetPublicId ()},
2023-03-15 11:40:40 -04:00
res : & pbs . ListAuthMethodsResponse { Items : cpSorted ( wantOtherAuthMethods )},
2020-08-06 12:50:20 -07:00
},
{
2021-01-25 19:37:40 -05:00
name : "List No Auth Methods" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : oNoAuthMethods . GetPublicId ()},
res : & pbs . ListAuthMethodsResponse {},
2020-08-06 12:50:20 -07:00
},
{
2021-01-25 19:37:40 -05:00
name : "Unfound Auth Method" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : "o_DoesntExis" },
err : handlers . ApiErrorWithCode ( codes . NotFound ),
},
{
name : "List All Auth Methods Recursively" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : "global" , Recursive : true },
res : & pbs . ListAuthMethodsResponse {
2023-03-15 11:40:40 -04:00
Items : func () [] * pb . AuthMethod {
return cpSorted ( append ( wantSomeAuthMethods , wantOtherAuthMethods ... ))
}(),
2021-01-25 19:37:40 -05:00
},
2020-08-06 12:50:20 -07:00
},
2021-02-22 10:09:25 -07:00
{
name : "Filter To Some Auth Methods" ,
req : & pbs . ListAuthMethodsRequest {
ScopeId : "global" , Recursive : true ,
Filter : fmt . Sprintf ( `"/item/scope/id"==%q` , oWithAuthMethods . GetPublicId ()),
},
2023-03-15 11:40:40 -04:00
res : & pbs . ListAuthMethodsResponse { Items : cpSorted ( wantSomeAuthMethods )},
2021-02-22 10:09:25 -07:00
},
{
name : "Filter All Auth Methods" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : oWithAuthMethods . GetPublicId (), Filter : `"/item/id"=="nothingmatchesthis"` },
res : & pbs . ListAuthMethodsResponse {},
},
{
name : "Filter Bad Format" ,
req : & pbs . ListAuthMethodsRequest { ScopeId : oWithAuthMethods . GetPublicId (), Filter : `"//id/"=="bad"` },
err : handlers . InvalidArgumentErrorf ( "bad format" , nil ),
},
2020-08-06 12:50:20 -07:00
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
2023-06-06 17:09:08 -04:00
s , err := authmethods . NewService ( ctx , kmsCache , pwRepoFn , oidcRepoFn , iamRepoFn , atRepoFn , ldapRepoFn )
2020-08-06 12:50:20 -07:00
require . NoError ( err , "Couldn't create new auth_method service." )
2021-04-14 15:49:08 -05:00
// First check with non-anonymous user
2021-07-27 09:59:52 -07:00
got , gErr := s . ListAuthMethods ( requestauth . DisabledAuthTestContext ( iamRepoFn , tc . req . GetScopeId ()), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
2021-01-25 19:37:40 -05:00
assert . True ( errors . Is ( gErr , tc . err ), "ListAuthMethods() for scope %q got error %v, wanted %v" , tc . req . GetScopeId (), gErr , tc . err )
2021-04-08 20:54:32 -04:00
return
}
require . NoError ( gErr )
2022-04-08 11:40:59 -04:00
for _ , g := range got . Items {
if oidcAttrs := g . GetOidcAuthMethodsAttributes (); oidcAttrs != nil {
assert . NotEqual ( "secret" , oidcAttrs . ClientSecretHmac )
2021-04-08 20:54:32 -04:00
}
2020-09-23 08:10:27 -07:00
}
2023-03-15 11:40:40 -04:00
slices . SortFunc ( got . Items , sorterFn )
2023-10-31 13:44:17 +00:00
assert . Empty ( cmp . Diff (
got ,
tc . res ,
protocmp . Transform (),
2023-03-15 11:40:40 -04:00
protocmp . IgnoreFields ( & pb . OidcAuthMethodAttributes {}, "client_secret_hmac" ),
2023-10-31 13:44:17 +00:00
protocmp . IgnoreFields ( & pb . LdapAuthMethodAttributes {}, "bind_password_hmac" , "client_certificate_key_hmac" ),
cmpopts . SortSlices ( func ( a , b string ) bool {
return a < b
}),
cmpopts . SortSlices ( func ( a , b protocmp . Message ) bool {
return a . String () < b . String ()
}),
), "ListAuthMethods() for scope %q got response %q, wanted %q" , tc . req . GetScopeId (), got , tc . res )
2021-04-14 15:49:08 -05:00
// Now check with anonymous user
2022-10-28 15:52:06 -04:00
got , gErr = s . ListAuthMethods ( requestauth . DisabledAuthTestContext ( iamRepoFn , tc . req . GetScopeId (), requestauth . WithUserId ( globals . AnonymousUserId )), tc . req )
2021-04-14 15:49:08 -05:00
require . NoError ( gErr )
2021-05-04 19:57:35 -05:00
assert . Len ( got . Items , len ( tc . res . Items ))
2021-04-14 16:19:02 -05:00
for _ , g := range got . GetItems () {
2022-04-08 11:40:59 -04:00
assert . Nil ( g . Attrs )
2021-04-14 15:49:08 -05:00
assert . Nil ( g . CreatedTime )
assert . Nil ( g . UpdatedTime )
assert . Empty ( g . Version )
}
2020-08-06 12:50:20 -07:00
})
}
}
func TestDelete ( t * testing . T ) {
2021-07-29 18:52:56 -06:00
ctx := context . TODO ()
2020-08-06 12:50:20 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
2020-08-19 16:47:18 -04:00
wrapper := db . TestWrapper ( t )
2021-04-08 20:54:32 -04:00
kmsCache := kms . TestKms ( t , conn , wrapper )
2020-09-04 14:48:11 -07:00
iamRepoFn := func () ( * iam . Repository , error ) {
return iam . TestRepo ( t , conn , wrapper ), nil
}
2021-04-08 20:54:32 -04:00
oidcRepoFn := func () ( * oidc . Repository , error ) {
2021-07-29 18:52:56 -06:00
return oidc . NewRepository ( ctx , rw , rw , kmsCache )
2021-04-08 20:54:32 -04:00
}
2023-03-15 11:40:40 -04:00
ldapRepoFn := func () ( * ldap . Repository , error ) {
return ldap . NewRepository ( ctx , rw , rw , kmsCache )
}
2020-10-12 18:03:15 -04:00
pwRepoFn := func () ( * password . Repository , error ) {
2023-06-06 17:09:08 -04:00
return password . NewRepository ( ctx , rw , rw , kmsCache )
2020-08-06 12:50:20 -07:00
}
2020-10-12 18:03:15 -04:00
atRepoFn := func () ( * authtoken . Repository , error ) {
2023-06-06 17:09:08 -04:00
return authtoken . NewRepository ( ctx , rw , rw , kmsCache )
2020-10-12 18:03:15 -04:00
}
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
2020-08-06 12:50:20 -07:00
2020-08-19 16:47:18 -04:00
o , _ := iam . TestScopes ( t , iamRepo )
2021-04-08 20:54:32 -04:00
pwam := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]
2020-08-06 12:50:20 -07:00
2021-04-08 20:54:32 -04:00
databaseWrapper , err := kmsCache . GetWrapper ( context . Background (), o . GetPublicId (), kms . KeyPurposeDatabase )
require . NoError ( t , err )
oidcam := oidc . TestAuthMethod ( t , conn , databaseWrapper , o . GetPublicId (), oidc . InactiveState , "alice_rp" , "my-dogs-name" ,
2023-11-21 11:30:02 -05:00
oidc . WithIssuer ( oidc . TestConvertToUrls ( t , "https://alice.com" )[ 0 ]), oidc . WithApiUrl ( oidc . TestConvertToUrls ( t , "https://api.com" )[ 0 ]),
oidc . WithPrompts ( oidc . SelectAccount ))
2021-04-08 20:54:32 -04:00
2023-03-15 11:40:40 -04:00
ldapAm := ldap . TestAuthMethod ( t , conn , databaseWrapper , o . GetPublicId (), [] string { "ldaps://ldap1" })
2023-06-06 17:09:08 -04:00
s , err := authmethods . NewService ( ctx , kmsCache , pwRepoFn , oidcRepoFn , iamRepoFn , atRepoFn , ldapRepoFn )
2020-08-06 12:50:20 -07:00
require . NoError ( t , err , "Error when getting new auth_method service." )
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . DeleteAuthMethodRequest
res * pbs . DeleteAuthMethodResponse
err error
2020-08-06 12:50:20 -07:00
}{
{
2021-04-08 20:54:32 -04:00
name : "Delete an Existing PW AuthMethod" ,
req : & pbs . DeleteAuthMethodRequest {
Id : pwam . GetPublicId (),
},
2021-10-19 17:21:41 -04:00
res : & pbs . DeleteAuthMethodResponse {},
2021-04-08 20:54:32 -04:00
},
{
name : "Delete an Existing OIDC AuthMethod" ,
2020-08-06 12:50:20 -07:00
req : & pbs . DeleteAuthMethodRequest {
2021-04-08 20:54:32 -04:00
Id : oidcam . GetPublicId (),
2020-08-06 12:50:20 -07:00
},
2021-10-19 17:21:41 -04:00
res : & pbs . DeleteAuthMethodResponse {},
2020-08-06 12:50:20 -07:00
},
2023-03-15 11:40:40 -04:00
{
name : "Delete an Existing LDAP AuthMethod" ,
req : & pbs . DeleteAuthMethodRequest {
Id : ldapAm . GetPublicId (),
},
res : & pbs . DeleteAuthMethodResponse {},
},
2020-08-06 12:50:20 -07:00
{
name : "Delete bad auth_method id" ,
req : & pbs . DeleteAuthMethodRequest {
2023-03-10 15:06:39 -05:00
Id : globals . PasswordAuthMethodPrefix + "_doesntexis" ,
2020-08-06 12:50:20 -07:00
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-08-06 12:50:20 -07:00
},
{
name : "Bad AuthMethod Id formatting" ,
req : & pbs . DeleteAuthMethodRequest {
Id : "bad_format" ,
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2021-07-27 09:59:52 -07:00
got , gErr := s . DeleteAuthMethod ( requestauth . DisabledAuthTestContext ( iamRepoFn , o . GetPublicId ()), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "DeleteAuthMethod(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-06 12:50:20 -07:00
assert . EqualValuesf ( tc . res , got , "DeleteAuthMethod(%q) got response %q, wanted %q" , tc . req , got , tc . res )
})
}
}
func TestDelete_twice ( t * testing . T ) {
2021-07-29 18:52:56 -06:00
ctx := context . TODO ()
2020-08-06 12:50:20 -07:00
assert , require := assert . New ( t ), require . New ( t )
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
2020-08-19 16:47:18 -04:00
wrapper := db . TestWrapper ( t )
kms := kms . TestKms ( t , conn , wrapper )
2020-09-04 14:48:11 -07:00
iamRepoFn := func () ( * iam . Repository , error ) {
return iam . TestRepo ( t , conn , wrapper ), nil
}
2021-04-08 20:54:32 -04:00
oidcRepoFn := func () ( * oidc . Repository , error ) {
2021-07-29 18:52:56 -06:00
return oidc . NewRepository ( ctx , rw , rw , kms )
2021-04-08 20:54:32 -04:00
}
2023-03-15 11:40:40 -04:00
ldapRepoFn := func () ( * ldap . Repository , error ) {
return ldap . NewRepository ( ctx , rw , rw , kms )
}
2020-10-12 18:03:15 -04:00
pwRepoFn := func () ( * password . Repository , error ) {
2023-06-06 17:09:08 -04:00
return password . NewRepository ( ctx , rw , rw , kms )
2020-08-06 12:50:20 -07:00
}
2020-10-12 18:03:15 -04:00
atRepoFn := func () ( * authtoken . Repository , error ) {
2023-06-06 17:09:08 -04:00
return authtoken . NewRepository ( ctx , rw , rw , kms )
2020-10-12 18:03:15 -04:00
}
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
2020-08-06 12:50:20 -07:00
2020-08-19 16:47:18 -04:00
o , _ := iam . TestScopes ( t , iamRepo )
2020-08-06 12:50:20 -07:00
am := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]
2023-06-06 17:09:08 -04:00
s , err := authmethods . NewService ( ctx , kms , pwRepoFn , oidcRepoFn , iamRepoFn , atRepoFn , ldapRepoFn )
2020-08-06 12:50:20 -07:00
require . NoError ( err , "Error when getting new auth_method service." )
req := & pbs . DeleteAuthMethodRequest {
Id : am . GetPublicId (),
}
2021-07-27 09:59:52 -07:00
_ , gErr := s . DeleteAuthMethod ( requestauth . DisabledAuthTestContext ( iamRepoFn , o . GetPublicId ()), req )
2020-08-06 12:50:20 -07:00
assert . NoError ( gErr , "First attempt" )
2021-07-27 09:59:52 -07:00
_ , gErr = s . DeleteAuthMethod ( requestauth . DisabledAuthTestContext ( iamRepoFn , o . GetPublicId ()), req )
2020-09-04 14:48:11 -07:00
assert . Error ( gErr , "Second attempt" )
2020-09-23 08:10:27 -07:00
assert . True ( errors . Is ( gErr , handlers . ApiErrorWithCode ( codes . NotFound )), "Expected permission denied for the second delete." )
2020-08-06 12:50:20 -07:00
}
func TestCreate ( t * testing . T ) {
2021-07-29 18:52:56 -06:00
ctx := context . TODO ()
2020-08-06 12:50:20 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
2020-08-19 16:47:18 -04:00
wrapper := db . TestWrapper ( t )
2023-03-15 11:40:40 -04:00
testKms := kms . TestKms ( t , conn , wrapper )
2020-09-04 14:48:11 -07:00
iamRepoFn := func () ( * iam . Repository , error ) {
return iam . TestRepo ( t , conn , wrapper ), nil
}
2020-10-12 18:03:15 -04:00
pwRepoFn := func () ( * password . Repository , error ) {
2023-06-06 17:09:08 -04:00
return password . NewRepository ( ctx , rw , rw , testKms )
2020-08-06 12:50:20 -07:00
}
2021-04-08 20:54:32 -04:00
oidcRepoFn := func () ( * oidc . Repository , error ) {
2023-03-15 11:40:40 -04:00
return oidc . NewRepository ( ctx , rw , rw , testKms )
}
ldapRepoFn := func () ( * ldap . Repository , error ) {
return ldap . NewRepository ( ctx , rw , rw , testKms )
2021-04-08 20:54:32 -04:00
}
2020-10-12 18:03:15 -04:00
atRepoFn := func () ( * authtoken . Repository , error ) {
2023-06-06 17:09:08 -04:00
return authtoken . NewRepository ( ctx , rw , rw , testKms )
2020-10-12 18:03:15 -04:00
}
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
2020-08-06 12:50:20 -07:00
2020-08-19 16:47:18 -04:00
o , _ := iam . TestScopes ( t , iamRepo )
2020-08-06 12:50:20 -07:00
defaultAm := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]
2021-03-24 16:07:43 -04:00
defaultCreated := defaultAm . GetCreateTime (). GetTimestamp ()
2020-08-06 12:50:20 -07:00
2023-03-15 11:40:40 -04:00
_ , testEncodedCert := ldap . TestGenerateCA ( t , "localhost" )
_ , privKey , err := ed25519 . GenerateKey ( rand . Reader )
require . NoError ( t , err )
derEncodedKey , err := x509 . MarshalPKCS8PrivateKey ( privKey )
require . NoError ( t , err )
testEncodedKey := pem . EncodeToMemory ( & pem . Block { Type : "PRIVATE KEY" , Bytes : derEncodedKey })
2020-08-06 12:50:20 -07:00
cases := [] struct {
2023-03-15 11:40:40 -04:00
name string
req * pbs . CreateAuthMethodRequest
res * pbs . CreateAuthMethodResponse
idPrefix string
err error
errContains string
2020-08-06 12:50:20 -07:00
}{
{
2021-04-08 20:54:32 -04:00
name : "Create a valid Password AuthMethod" ,
2020-08-06 12:50:20 -07:00
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2020-08-06 12:50:20 -07:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
Type : "password" ,
}},
2023-03-10 15:06:39 -05:00
idPrefix : globals . PasswordAuthMethodPrefix + "_" ,
2020-08-06 12:50:20 -07:00
res : & pbs . CreateAuthMethodResponse {
2023-03-10 15:06:39 -05:00
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . PasswordAuthMethodPrefix ),
2020-08-06 12:50:20 -07:00
Item : & pb . AuthMethod {
Id : defaultAm . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2020-08-06 12:50:20 -07:00
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
2021-03-25 03:15:43 -04:00
Scope : & scopepb . ScopeInfo { Id : o . GetPublicId (), Type : o . GetType (), ParentScopeId : scope . Global . String ()},
2020-08-11 11:19:29 -04:00
Version : 1 ,
2020-08-06 12:50:20 -07:00
Type : "password" ,
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_PasswordAuthMethodAttributes {
PasswordAuthMethodAttributes : & pb . PasswordAuthMethodAttributes {
MinPasswordLength : 8 ,
MinLoginNameLength : 3 ,
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : pwAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-08-06 12:50:20 -07:00
},
},
},
2020-09-03 08:02:30 -07:00
{
2021-04-08 20:54:32 -04:00
name : "Create a valid OIDC AuthMethod" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ApiUrlPrefix : wrapperspb . String ( "https://callback.prefix:9281/path" ),
AllowedAudiences : [] string { "foo" , "bar" },
ClaimsScopes : [] string { "email" , "profile" },
AccountClaimMaps : [] string { "display_name=name" , "oid=sub" },
},
},
2021-04-08 20:54:32 -04:00
}},
2023-03-10 15:06:39 -05:00
idPrefix : globals . OidcAuthMethodPrefix + "_" ,
2021-04-08 20:54:32 -04:00
res : & pbs . CreateAuthMethodResponse {
2023-03-10 15:06:39 -05:00
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . OidcAuthMethodPrefix ),
2021-04-08 20:54:32 -04:00
Item : & pb . AuthMethod {
Id : defaultAm . GetPublicId (),
ScopeId : o . GetPublicId (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : o . GetPublicId (), Type : o . GetType (), ParentScopeId : scope . Global . String ()},
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . InactiveState ),
ApiUrlPrefix : wrapperspb . String ( "https://callback.prefix:9281/path" ),
CallbackUrl : "https://callback.prefix:9281/path/v1/auth-methods/oidc:authenticate:callback" ,
AllowedAudiences : [] string { "foo" , "bar" },
ClaimsScopes : [] string { "email" , "profile" },
AccountClaimMaps : [] string { "display_name=name" , "oid=sub" },
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : oidcAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
},
},
},
2023-03-15 11:40:40 -04:00
{
name : "create-a-valid-ldap-auth-method" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
StartTls : true ,
InsecureTls : true ,
DiscoverDn : true ,
AnonGroupSearch : true ,
UpnDomain : wrapperspb . String ( "upn_domain" ),
Urls : [] string { "ldap://ldap1" , "ldaps://ldap1" },
BindDn : wrapperspb . String ( "bind-dn" ),
BindPassword : wrapperspb . String ( "bind-password" ),
UserDn : wrapperspb . String ( "user-dn" ),
UserAttr : wrapperspb . String ( "user-attr" ),
UserFilter : wrapperspb . String ( "user-filter" ),
EnableGroups : true ,
GroupDn : wrapperspb . String ( "group-dn" ),
GroupAttr : wrapperspb . String ( "group-attr" ),
GroupFilter : wrapperspb . String ( "group-filter" ),
Certificates : [] string { testEncodedCert },
ClientCertificate : wrapperspb . String ( testEncodedCert ),
ClientCertificateKey : wrapperspb . String ( string ( testEncodedKey )),
UseTokenGroups : true ,
AccountAttributeMaps : [] string { "mail=email" },
2023-09-06 15:07:45 -04:00
MaximumPageSize : 10 ,
DereferenceAliases : wrapperspb . String ( string ( ldap . DerefAlways )),
2023-03-15 11:40:40 -04:00
},
},
}},
idPrefix : globals . LdapAuthMethodPrefix + "_" ,
res : & pbs . CreateAuthMethodResponse {
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . LdapAuthMethodPrefix ),
Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : o . GetPublicId (), Type : o . GetType (), ParentScopeId : scope . Global . String ()},
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
State : string ( ldap . InactiveState ),
StartTls : true ,
InsecureTls : true ,
DiscoverDn : true ,
AnonGroupSearch : true ,
UpnDomain : wrapperspb . String ( "upn_domain" ),
Urls : [] string { "ldap://ldap1" , "ldaps://ldap1" },
BindDn : wrapperspb . String ( "bind-dn" ),
UserDn : wrapperspb . String ( "user-dn" ),
UserAttr : wrapperspb . String ( "user-attr" ),
UserFilter : wrapperspb . String ( "user-filter" ),
EnableGroups : true ,
GroupDn : wrapperspb . String ( "group-dn" ),
GroupAttr : wrapperspb . String ( "group-attr" ),
GroupFilter : wrapperspb . String ( "group-filter" ),
Certificates : [] string { testEncodedCert },
ClientCertificate : wrapperspb . String ( testEncodedCert ),
UseTokenGroups : true ,
AccountAttributeMaps : [] string { "mail=email" },
2023-09-06 15:07:45 -04:00
MaximumPageSize : 10 ,
DereferenceAliases : wrapperspb . String ( string ( ldap . DerefAlways )),
2023-03-15 11:40:40 -04:00
},
},
AuthorizedActions : ldapAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
},
},
},
2021-04-08 20:54:32 -04:00
{
name : "Create a global Password AuthMethod" ,
2020-09-03 08:02:30 -07:00
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : scope . Global . String (),
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
Type : "password" ,
}},
2023-03-10 15:06:39 -05:00
idPrefix : globals . PasswordAuthMethodPrefix + "_" ,
2020-09-03 08:02:30 -07:00
res : & pbs . CreateAuthMethodResponse {
2023-03-10 15:06:39 -05:00
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . PasswordAuthMethodPrefix ),
2020-09-03 08:02:30 -07:00
Item : & pb . AuthMethod {
Id : defaultAm . GetPublicId (),
ScopeId : scope . Global . String (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
2021-03-25 03:15:43 -04:00
Scope : & scopepb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
2020-09-03 08:02:30 -07:00
Version : 1 ,
Type : "password" ,
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_PasswordAuthMethodAttributes {
PasswordAuthMethodAttributes : & pb . PasswordAuthMethodAttributes {
MinPasswordLength : 8 ,
MinLoginNameLength : 3 ,
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : pwAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
},
},
},
{
name : "Create a global OIDC AuthMethod" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : scope . Global . String (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url" ),
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
},
},
2021-04-08 20:54:32 -04:00
}},
2023-03-10 15:06:39 -05:00
idPrefix : globals . OidcAuthMethodPrefix + "_" ,
2021-04-08 20:54:32 -04:00
res : & pbs . CreateAuthMethodResponse {
2023-03-10 15:06:39 -05:00
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . OidcAuthMethodPrefix ),
2021-04-08 20:54:32 -04:00
Item : & pb . AuthMethod {
Id : defaultAm . GetPublicId (),
ScopeId : scope . Global . String (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . InactiveState ),
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : oidcAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-09-03 08:02:30 -07:00
},
},
},
2023-03-15 11:40:40 -04:00
{
name : "create-a-global-ldap-auth-method" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : scope . Global . String (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" , "ldaps://ldap1" },
EnableGroups : true ,
GroupDn : wrapperspb . String ( "group-dn" ),
},
},
}},
idPrefix : globals . LdapAuthMethodPrefix + "_" ,
res : & pbs . CreateAuthMethodResponse {
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . LdapAuthMethodPrefix ),
Item : & pb . AuthMethod {
ScopeId : scope . Global . String (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
State : string ( ldap . InactiveState ),
Urls : [] string { "ldap://ldap1" , "ldaps://ldap1" },
EnableGroups : true ,
GroupDn : wrapperspb . String ( "group-dn" ),
},
},
AuthorizedActions : ldapAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
},
},
},
2020-08-06 12:50:20 -07:00
{
name : "Can't specify Id" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2023-03-10 15:06:39 -05:00
Id : globals . PasswordAuthMethodPrefix + "_notallowed" ,
2020-09-02 19:43:41 -07:00
Type : "password" ,
2020-08-06 12:50:20 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
name : "Can't specify Created Time" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2021-03-24 16:07:43 -04:00
CreatedTime : timestamppb . Now (),
2023-11-02 16:03:47 -04:00
Type : password . Subtype . String (),
2020-08-06 12:50:20 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
name : "Can't specify Update Time" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2021-03-24 16:07:43 -04:00
UpdatedTime : timestamppb . Now (),
2023-11-02 16:03:47 -04:00
Type : password . Subtype . String (),
2021-04-08 20:54:32 -04:00
}},
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
},
{
name : "Can't specify IsPrimary" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : password . Subtype . String (),
2021-04-08 20:54:32 -04:00
IsPrimary : true ,
2020-08-06 12:50:20 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
name : "Can't specify Update Time" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2021-03-24 16:07:43 -04:00
UpdatedTime : timestamppb . Now (),
2023-11-02 16:03:47 -04:00
Type : password . Subtype . String (),
2020-08-06 12:50:20 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
name : "Must specify type" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2020-08-06 12:50:20 -07:00
Name : & wrapperspb . StringValue { Value : "must specify type" },
Description : & wrapperspb . StringValue { Value : "must specify type" },
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Doesn't Require Issuer" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
},
},
2021-04-08 20:54:32 -04:00
}},
res : & pbs . CreateAuthMethodResponse {
2023-03-10 15:06:39 -05:00
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . OidcAuthMethodPrefix ),
2020-08-06 12:50:20 -07:00
Item : & pb . AuthMethod {
2021-04-08 20:54:32 -04:00
Id : defaultAm . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : o . GetPublicId (),
2021-04-08 20:54:32 -04:00
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : o . GetPublicId (), Type : o . GetType (), ParentScopeId : scope . Global . String ()},
Version : 1 ,
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . InactiveState ),
},
},
2021-04-08 20:54:32 -04:00
AuthorizedActions : oidcAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : authorizedCollectionActions ,
2020-08-06 12:50:20 -07:00
},
},
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Requires ApiUrl" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Requires Client Id" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientSecret : wrapperspb . String ( "secret" ),
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Requires Client Secret" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "someclientid" ),
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
2020-09-08 16:04:19 -07:00
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod cant specify client secret hmac" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ClientSecretHmac : "hmac" ,
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-08 16:04:19 -07:00
},
2020-08-06 12:50:20 -07:00
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod cant specify state" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
State : string ( oidc . InactiveState ),
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Must Match Standard Alg Names" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example2.discovery.url:4821" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
SigningAlgorithms : [] string { string ( oidc . ES256 ), strings . ToLower ( string ( oidc . EdDSA ))},
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod API Urls Prefix Format" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example2.discovery.url:4821" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ApiUrlPrefix : wrapperspb . String ( "invalid path" ),
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod Callback Url Read Only" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example2.discovery.url:4821" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
CallbackUrl : "http://another.url.com:82471" ,
},
},
2021-04-08 20:54:32 -04:00
}},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-06 12:50:20 -07:00
},
2020-08-14 14:08:12 -07:00
{
2021-04-08 20:54:32 -04:00
name : "OIDC AuthMethod unparseable certificates" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example2.discovery.url:4821" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
IdpCaCerts : [] string { "unparseable" },
},
},
2021-04-08 20:54:32 -04:00
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-08-14 14:08:12 -07:00
},
2021-04-28 06:53:13 -04:00
{
name : "OIDC AuthMethod cant specify default claims scopes of openid" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : oidc . Subtype . String (),
2022-04-08 11:40:59 -04:00
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ClaimsScopes : [] string { "openid" },
},
},
2021-04-28 06:53:13 -04:00
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
},
2023-03-15 11:40:40 -04:00
{
name : "ldap-auth-method-requires-urls" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "At least one URL is required" ,
},
{
name : "ldap-auth-method-invalid-urls" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" , "not-ldap-scheme://ldap2" },
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "is not either ldap or ldaps" ,
},
2023-09-06 15:07:45 -04:00
{
name : "ldap-auth-method-invalid-deref-aliases" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-09-06 15:07:45 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
DereferenceAliases : wrapperspb . String ( "invalid" ),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "invalid is not a valid attributes.dereference_aliases" ,
},
2023-03-15 11:40:40 -04:00
{
name : "ldap-auth-method-invalid-cert" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
Certificates : [] string { "invalid-cert" },
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "failed to parse certificate: invalid PEM encoding" ,
},
{
name : "ldap-auth-method-missing-bind-dn" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
BindPassword : wrapperspb . String ( "pass" ),
},
},
}},
2023-07-27 15:41:37 -04:00
errContains : "missing dn" ,
2023-03-15 11:40:40 -04:00
},
{
name : "ldap-auth-method-missing-bind-password" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
BindDn : wrapperspb . String ( "bind-dn" ),
},
},
}},
2023-07-27 15:41:37 -04:00
errContains : "missing password" ,
2023-03-15 11:40:40 -04:00
},
{
name : "ldap-auth-method-invalid-client-cert" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
ClientCertificate : wrapperspb . String ( "invalid-cert" ),
ClientCertificateKey : wrapperspb . String ( string ( testEncodedKey )),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "failed to parse certificate: invalid PEM encoding" ,
},
{
name : "ldap-auth-method-invalid-client-cert-key" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
ClientCertificate : wrapperspb . String ( testEncodedCert ),
ClientCertificateKey : wrapperspb . String ( "invalid-key" ),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "attributes.client_certificate_key is not encoded as a valid pem" ,
},
{
name : "ldap-auth-method-client-cert-key-not-a-key" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
ClientCertificate : wrapperspb . String ( testEncodedCert ),
ClientCertificateKey : wrapperspb . String ( testEncodedCert ),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "attributes.client_certificate_key is not a valid private key" ,
},
{
name : "ldap-auth-method-missing-client-cert-key" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
ClientCertificate : wrapperspb . String ( testEncodedCert ),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "attributes.client_certificate is missing required attributes.client_certificate_key field" ,
},
{
name : "ldap-auth-method-missing-client-cert" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
ClientCertificateKey : wrapperspb . String ( string ( testEncodedKey )),
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "attributes.client_certificate_key is missing required attributes.client_certificate field" ,
},
{
name : "ldap-auth-method-invalid-attribute-map" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
2023-11-02 16:03:47 -04:00
Type : ldap . Subtype . String (),
2023-03-15 11:40:40 -04:00
Attrs : & pb . AuthMethod_LdapAuthMethodsAttributes {
LdapAuthMethodsAttributes : & pb . LdapAuthMethodAttributes {
Urls : [] string { "ldap://ldap1" },
AccountAttributeMaps : [] string { "invalid-map" },
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "invalid attributes.account_attribute_maps (unable to parse)" ,
},
2023-11-21 11:30:02 -05:00
{
name : "OIDC AuthMethod With Unsupported Prompt" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
Type : oidc . Subtype . String (),
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
ApiUrlPrefix : wrapperspb . String ( "https://api.com" ),
Issuer : wrapperspb . String ( "https://example2.discovery.url:4821" ),
ClientId : wrapperspb . String ( "someclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
Prompts : [] string { string ( oidc . SelectAccount ), "invalid" },
},
},
}},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "Contains unsupported prompt" ,
},
{
name : "Create OIDC AuthMethod With Supported Prompt" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
Type : oidc . Subtype . String (),
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "exampleclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ApiUrlPrefix : wrapperspb . String ( "https://callback.prefix:9281/path" ),
Prompts : [] string { string ( oidc . SelectAccount )},
},
},
}},
idPrefix : globals . OidcAuthMethodPrefix + "_" ,
res : & pbs . CreateAuthMethodResponse {
Uri : fmt . Sprintf ( "auth-methods/%s_" , globals . OidcAuthMethodPrefix ),
Item : & pb . AuthMethod {
Id : defaultAm . GetPublicId (),
ScopeId : o . GetPublicId (),
CreatedTime : defaultAm . GetCreateTime (). GetTimestamp (),
UpdatedTime : defaultAm . GetUpdateTime (). GetTimestamp (),
Scope : & scopepb . ScopeInfo { Id : o . GetPublicId (), Type : o . GetType (), ParentScopeId : scope . Global . String ()},
Version : 1 ,
Type : oidc . Subtype . String (),
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/" ),
ClientId : wrapperspb . String ( "exampleclientid" ),
ClientSecretHmac : "<hmac>" ,
State : string ( oidc . InactiveState ),
ApiUrlPrefix : wrapperspb . String ( "https://callback.prefix:9281/path" ),
CallbackUrl : "https://callback.prefix:9281/path/v1/auth-methods/oidc:authenticate:callback" ,
Prompts : [] string { string ( oidc . SelectAccount )},
},
},
AuthorizedActions : oidcAuthorizedActions ,
AuthorizedCollectionActions : authorizedCollectionActions ,
},
},
},
2023-12-08 09:51:54 -05:00
{
name : "Create OIDC AuthMethod with none and other prompts" ,
req : & pbs . CreateAuthMethodRequest { Item : & pb . AuthMethod {
ScopeId : o . GetPublicId (),
Type : oidc . Subtype . String (),
Attrs : & pb . AuthMethod_OidcAuthMethodsAttributes {
OidcAuthMethodsAttributes : & pb . OidcAuthMethodAttributes {
Issuer : wrapperspb . String ( "https://example.discovery.url:4821/.well-known/openid-configuration/" ),
ClientId : wrapperspb . String ( "exampleclientid" ),
ClientSecret : wrapperspb . String ( "secret" ),
ApiUrlPrefix : wrapperspb . String ( "https://callback.prefix:9281/path" ),
Prompts : [] string { string ( oidc . None ), string ( oidc . SelectAccount )},
},
},
}},
idPrefix : globals . OidcAuthMethodPrefix + "_" ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
errContains : "includes \\\"none\\\" with other values" ,
},
2020-08-06 12:50:20 -07:00
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
2023-06-06 17:09:08 -04:00
s , err := authmethods . NewService ( ctx , testKms , pwRepoFn , oidcRepoFn , iamRepoFn , atRepoFn , ldapRepoFn )
2021-04-08 20:54:32 -04:00
require . NoError ( err , "Error when getting new auth_method service." )
2020-08-06 12:50:20 -07:00
2021-07-27 09:59:52 -07:00
got , gErr := s . CreateAuthMethod ( requestauth . DisabledAuthTestContext ( iamRepoFn , tc . req . GetItem (). GetScopeId ()), tc . req )
2023-07-27 15:41:37 -04:00
switch {
case tc . err != nil :
2020-09-23 08:10:27 -07:00
require . Error ( gErr )
2021-04-08 20:54:32 -04:00
assert . True ( errors . Is ( gErr , tc . err ), "CreateAuthMethod(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
2023-03-15 11:40:40 -04:00
if tc . errContains != "" {
assert . Contains ( gErr . Error (), tc . errContains )
}
2021-04-08 20:54:32 -04:00
return
2023-07-27 15:41:37 -04:00
case tc . errContains != "" :
require . Error ( gErr )
assert . Contains ( gErr . Error (), tc . errContains )
return
2020-09-23 08:10:27 -07:00
}
2023-07-27 15:41:37 -04:00
2021-04-08 20:54:32 -04:00
require . NoError ( gErr )
2020-08-06 12:50:20 -07:00
if tc . res == nil {
require . Nil ( got )
}
2023-10-31 13:44:17 +00:00
cmpOptions := [] cmp . Option {
protocmp . Transform (),
cmpopts . SortSlices ( func ( a , b string ) bool {
return a < b
}),
cmpopts . SortSlices ( func ( a , b protocmp . Message ) bool {
return a . String () < b . String ()
}),
}
2020-08-06 12:50:20 -07:00
if got != nil {
2021-04-08 20:54:32 -04:00
assert . Contains ( got . GetUri (), tc . res . Uri )
assert . True ( strings . HasPrefix ( got . GetItem (). GetId (), tc . idPrefix ))
gotCreateTime := got . GetItem (). GetCreatedTime ()
gotUpdateTime := got . GetItem (). GetUpdatedTime ()
2020-08-06 12:50:20 -07:00
2021-04-08 20:54:32 -04:00
// Verify it is a auth_method created after the test setup's default auth_method
assert . True ( gotCreateTime . AsTime (). After ( defaultCreated . AsTime ()), "New auth_method should have been created after default auth_method. Was created %v, which is after %v" , gotCreateTime , defaultCreated )
assert . True ( gotUpdateTime . AsTime (). After ( defaultCreated . AsTime ()), "New auth_method should have been updated after default auth_method. Was updated %v, which is after %v" , gotUpdateTime , defaultCreated )
2020-08-06 12:50:20 -07:00
2022-04-08 11:40:59 -04:00
// Ignore all values which are hard to compare against.
cmpOptions = append (
cmpOptions ,
protocmp . IgnoreFields ( & pbs . CreateAuthMethodResponse {}, "uri" ),
protocmp . IgnoreFields ( & pb . AuthMethod {}, "id" , "created_time" , "updated_time" ),
)
if oidcAttrs := got . Item . GetOidcAuthMethodsAttributes (); oidcAttrs != nil {
assert . NotEqual ( oidcAttrs . ClientSecret , oidcAttrs . ClientSecretHmac )
exp := tc . res . Item . Attrs .( * pb . AuthMethod_OidcAuthMethodsAttributes ). OidcAuthMethodsAttributes . CallbackUrl
gVal := oidcAttrs . CallbackUrl
2021-04-08 20:54:32 -04:00
matches , err := regexp . MatchString ( exp , gVal )
require . NoError ( err )
assert . True ( matches , "%q doesn't match %q" , gVal , exp )
2022-04-08 11:40:59 -04:00
cmpOptions = append (
cmpOptions ,
protocmp . IgnoreFields ( & pb . OidcAuthMethodAttributes {}, "client_secret_hmac" , "callback_url" ),
)
2021-06-11 11:43:36 -07:00
}
2023-03-15 11:40:40 -04:00
if ldapAttrs := got . Item . GetLdapAuthMethodsAttributes (); ldapAttrs != nil {
assert . NotEqual ( ldapAttrs . BindPassword , ldapAttrs . BindPasswordHmac )
cmpOptions = append (
cmpOptions ,
protocmp . SortRepeatedFields ( & pb . LdapAuthMethodAttributes {}, "account_attribute_maps" , "urls" , "certificates" ),
protocmp . IgnoreFields ( & pb . LdapAuthMethodAttributes {}, "bind_password_hmac" , "client_certificate_key_hmac" ),
)
}
2020-10-12 18:03:15 -04:00
}
2022-04-08 11:40:59 -04:00
assert . Empty ( cmp . Diff ( got , tc . res , cmpOptions ... ), "CreateAuthMethod(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-10-12 18:03:15 -04:00
})
}
}