2023-10-25 15:10:45 +00:00
|
|
|
package start
|
|
|
|
|
|
|
|
import (
|
2024-02-16 16:04:42 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-10-25 15:10:45 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-28 08:55:54 +00:00
|
|
|
"github.com/muhlemmer/gu"
|
2023-10-25 15:10:45 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-02-16 16:04:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-25 15:10:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/actions"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2024-02-28 08:55:54 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2023-10-25 15:10:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMustNewConfig(t *testing.T) {
|
2024-02-16 16:04:42 +00:00
|
|
|
encodedKey := "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF6aStGRlNKTDdmNXl3NEtUd3pnTQpQMzRlUEd5Y20vTStrVDBNN1Y0Q2d4NVYzRWFESXZUUUtUTGZCYUVCNDV6YjlMdGpJWHpEdzByWFJvUzJoTzZ0CmgrQ1lRQ3ozS0N2aDA5QzBJenhaaUIySVMzSC9hVCs1Qng5RUZZK3ZuQWtaamNjYnlHNVlOUnZtdE9sbnZJZUkKSDdxWjB0RXdrUGZGNUdFWk5QSlB0bXkzVUdWN2lvZmRWUVMxeFJqNzMrYU13NXJ2SDREOElkeWlBQzNWZWtJYgpwdDBWajBTVVgzRHdLdG9nMzM3QnpUaVBrM2FYUkYwc2JGaFFvcWRKUkk4TnFnWmpDd2pxOXlmSTV0eXhZc3duCitKR3pIR2RIdlczaWRPRGxtd0V0NUsycGFzaVJJV0syT0dmcSt3MEVjbHRRSGFidXFFUGdabG1oQ2tSZE5maXgKQndJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg=="
|
|
|
|
decodedKey, err := base64.StdEncoding.DecodeString(encodedKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-10-25 15:10:45 +00:00
|
|
|
type args struct {
|
|
|
|
yaml string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2024-02-16 16:04:42 +00:00
|
|
|
want func(*testing.T, *Config)
|
2023-10-25 15:10:45 +00:00
|
|
|
}{{
|
2024-02-16 16:04:42 +00:00
|
|
|
name: "actions deny list ok",
|
|
|
|
args: args{
|
|
|
|
yaml: `
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList:
|
|
|
|
- localhost
|
|
|
|
- 127.0.0.1
|
|
|
|
- foobar
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.Actions.HTTP.DenyList, []actions.AddressChecker{
|
|
|
|
&actions.DomainChecker{Domain: "localhost"},
|
|
|
|
&actions.IPChecker{IP: net.ParseIP("127.0.0.1")},
|
|
|
|
&actions.DomainChecker{Domain: "foobar"}})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "actions deny list string ok",
|
|
|
|
args: args{
|
|
|
|
yaml: `
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: localhost,127.0.0.1,foobar
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.Actions.HTTP.DenyList, []actions.AddressChecker{
|
|
|
|
&actions.DomainChecker{Domain: "localhost"},
|
|
|
|
&actions.IPChecker{IP: net.ParseIP("127.0.0.1")},
|
|
|
|
&actions.DomainChecker{Domain: "foobar"}})
|
|
|
|
},
|
|
|
|
}, {
|
2023-10-25 15:10:45 +00:00
|
|
|
name: "features ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
DefaultInstance:
|
|
|
|
Features:
|
2024-02-28 08:55:54 +00:00
|
|
|
LoginDefaultOrg: true
|
|
|
|
LegacyIntrospection: true
|
|
|
|
TriggerIntrospectionProjections: true
|
2024-03-12 13:50:13 +00:00
|
|
|
UserSchema: true
|
2024-02-16 16:04:42 +00:00
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
2023-10-25 15:10:45 +00:00
|
|
|
`},
|
2024-02-16 16:04:42 +00:00
|
|
|
want: func(t *testing.T, config *Config) {
|
2024-02-28 08:55:54 +00:00
|
|
|
assert.Equal(t, config.DefaultInstance.Features, &command.InstanceFeatures{
|
|
|
|
LoginDefaultOrg: gu.Ptr(true),
|
|
|
|
LegacyIntrospection: gu.Ptr(true),
|
|
|
|
TriggerIntrospectionProjections: gu.Ptr(true),
|
2024-03-12 13:50:13 +00:00
|
|
|
UserSchema: gu.Ptr(true),
|
2024-02-16 16:04:42 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "system api users ok",
|
2023-10-25 15:10:45 +00:00
|
|
|
args: args{yaml: `
|
|
|
|
SystemAPIUsers:
|
|
|
|
- superuser:
|
|
|
|
Memberships:
|
|
|
|
- MemberType: System
|
|
|
|
- MemberType: Organization
|
|
|
|
- MemberType: IAM
|
2024-02-16 16:04:42 +00:00
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
2023-10-25 15:10:45 +00:00
|
|
|
`},
|
2024-02-16 16:04:42 +00:00
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.SystemAPIUsers, map[string]*authz.SystemAPIUser{
|
2023-10-25 15:10:45 +00:00
|
|
|
"superuser": {
|
|
|
|
Memberships: authz.Memberships{{
|
|
|
|
MemberType: authz.MemberTypeSystem,
|
|
|
|
}, {
|
|
|
|
MemberType: authz.MemberTypeOrganization,
|
|
|
|
}, {
|
|
|
|
MemberType: authz.MemberTypeIAM,
|
|
|
|
}},
|
|
|
|
},
|
2024-02-16 16:04:42 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "system api users string ok",
|
|
|
|
args: args{yaml: fmt.Sprintf(`
|
|
|
|
SystemAPIUsers: >
|
|
|
|
{"systemuser": {"path": "/path/to/superuser/key.pem"}, "systemuser2": {"keyData": "%s"}}
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`, encodedKey)},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.SystemAPIUsers, map[string]*authz.SystemAPIUser{
|
|
|
|
"systemuser": {
|
|
|
|
Path: "/path/to/superuser/key.pem",
|
|
|
|
},
|
|
|
|
"systemuser2": {
|
|
|
|
KeyData: decodedKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "headers ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
Telemetry:
|
|
|
|
Headers:
|
|
|
|
single-value: single-value
|
|
|
|
multi-value:
|
|
|
|
- multi-value1
|
|
|
|
- multi-value2
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.Telemetry.Headers, http.Header{
|
|
|
|
"single-value": []string{"single-value"},
|
|
|
|
"multi-value": []string{"multi-value1", "multi-value2"},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "headers string ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
Telemetry:
|
|
|
|
Headers: >
|
|
|
|
{"single-value": "single-value", "multi-value": ["multi-value1", "multi-value2"]}
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.Telemetry.Headers, http.Header{
|
|
|
|
"single-value": []string{"single-value"},
|
|
|
|
"multi-value": []string{"multi-value1", "multi-value2"},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "message texts ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
DefaultInstance:
|
|
|
|
MessageTexts:
|
|
|
|
- MessageTextType: InitCode
|
|
|
|
Title: foo
|
|
|
|
- MessageTextType: PasswordReset
|
|
|
|
Greeting: bar
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.DefaultInstance.MessageTexts, []*domain.CustomMessageText{{
|
|
|
|
MessageTextType: "InitCode",
|
|
|
|
Title: "foo",
|
|
|
|
}, {
|
|
|
|
MessageTextType: "PasswordReset",
|
|
|
|
Greeting: "bar",
|
|
|
|
}})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "message texts string ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
DefaultInstance:
|
|
|
|
MessageTexts: >
|
|
|
|
[{"messageTextType": "InitCode", "title": "foo"}, {"messageTextType": "PasswordReset", "greeting": "bar"}]
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.DefaultInstance.MessageTexts, []*domain.CustomMessageText{{
|
|
|
|
MessageTextType: "InitCode",
|
|
|
|
Title: "foo",
|
|
|
|
}, {
|
|
|
|
MessageTextType: "PasswordReset",
|
|
|
|
Greeting: "bar",
|
|
|
|
}})
|
2023-10-25 15:10:45 +00:00
|
|
|
},
|
2024-04-14 09:55:54 +00:00
|
|
|
}, {
|
|
|
|
name: "roles ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
InternalAuthZ:
|
|
|
|
RolePermissionMappings:
|
|
|
|
- Role: IAM_OWNER
|
|
|
|
Permissions:
|
|
|
|
- iam.write
|
|
|
|
- Role: ORG_OWNER
|
|
|
|
Permissions:
|
|
|
|
- org.write
|
|
|
|
- org.read
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.InternalAuthZ, authz.Config{
|
|
|
|
RolePermissionMappings: []authz.RoleMapping{
|
|
|
|
{Role: "IAM_OWNER", Permissions: []string{"iam.write"}},
|
|
|
|
{Role: "ORG_OWNER", Permissions: []string{"org.write", "org.read"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "roles string ok",
|
|
|
|
args: args{yaml: `
|
|
|
|
InternalAuthZ:
|
|
|
|
RolePermissionMappings: >
|
|
|
|
[{"role": "IAM_OWNER", "permissions": ["iam.write"]}, {"role": "ORG_OWNER", "permissions": ["org.write", "org.read"]}]
|
|
|
|
Log:
|
|
|
|
Level: info
|
|
|
|
Actions:
|
|
|
|
HTTP:
|
|
|
|
DenyList: []
|
|
|
|
`},
|
|
|
|
want: func(t *testing.T, config *Config) {
|
|
|
|
assert.Equal(t, config.InternalAuthZ, authz.Config{
|
|
|
|
RolePermissionMappings: []authz.RoleMapping{
|
|
|
|
{Role: "IAM_OWNER", Permissions: []string{"iam.write"}},
|
|
|
|
{Role: "ORG_OWNER", Permissions: []string{"org.write", "org.read"}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2023-10-25 15:10:45 +00:00
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
v := viper.New()
|
|
|
|
v.SetConfigType("yaml")
|
2024-02-16 16:04:42 +00:00
|
|
|
require.NoError(t, v.ReadConfig(strings.NewReader(tt.args.yaml)))
|
2023-10-25 15:10:45 +00:00
|
|
|
got := MustNewConfig(v)
|
2024-02-16 16:04:42 +00:00
|
|
|
tt.want(t, got)
|
2023-10-25 15:10:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|