2021-03-15 11:51:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-16 15:49:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
|
|
|
id_mock "github.com/zitadel/zitadel/internal/id/mock"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2021-03-15 11:51:15 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func TestAddAPIConfig(t *testing.T) {
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
type fields struct {
|
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
type args struct {
|
|
|
|
a *project.Aggregate
|
|
|
|
appID string
|
|
|
|
name string
|
|
|
|
filter preparation.FilterToQueryReducer
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
agg := project.NewAggregate("test", "test")
|
|
|
|
|
|
|
|
tests := []struct {
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want Want
|
2022-04-12 14:20:17 +00:00
|
|
|
}{
|
|
|
|
{
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
name: "invalid appID",
|
|
|
|
fields: fields{},
|
2022-04-12 14:20:17 +00:00
|
|
|
args: args{
|
|
|
|
a: agg,
|
|
|
|
appID: "",
|
|
|
|
name: "name",
|
|
|
|
},
|
|
|
|
want: Want{
|
|
|
|
ValidationErr: errors.ThrowInvalidArgument(nil, "PROJE-XHsKt", "Errors.Invalid.Argument"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
name: "invalid name",
|
|
|
|
fields: fields{},
|
2022-04-12 14:20:17 +00:00
|
|
|
args: args{
|
|
|
|
a: agg,
|
|
|
|
appID: "appID",
|
|
|
|
name: "",
|
|
|
|
},
|
|
|
|
want: Want{
|
|
|
|
ValidationErr: errors.ThrowInvalidArgument(nil, "PROJE-F7g21", "Errors.Invalid.Argument"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
name: "project not exists",
|
|
|
|
fields: fields{},
|
2022-04-12 14:20:17 +00:00
|
|
|
args: args{
|
|
|
|
a: agg,
|
|
|
|
appID: "id",
|
|
|
|
name: "name",
|
|
|
|
filter: NewMultiFilter().
|
|
|
|
Append(func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return nil, nil
|
|
|
|
}).
|
|
|
|
Filter(),
|
|
|
|
},
|
|
|
|
want: Want{
|
|
|
|
CreateErr: errors.ThrowNotFound(nil, "PROJE-Sf2gb", "Errors.Project.NotFound"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct without client secret",
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
fields: fields{
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "clientID"),
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
args: args{
|
|
|
|
a: agg,
|
|
|
|
appID: "appID",
|
|
|
|
name: "name",
|
|
|
|
filter: NewMultiFilter().
|
|
|
|
Append(func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
return []eventstore.Event{
|
|
|
|
project.NewProjectAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&agg.Aggregate,
|
|
|
|
"project",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
domain.PrivateLabelingSettingUnspecified,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}).
|
|
|
|
Filter(),
|
|
|
|
},
|
|
|
|
want: Want{
|
|
|
|
Commands: []eventstore.Command{
|
|
|
|
project.NewApplicationAddedEvent(
|
|
|
|
ctx,
|
|
|
|
&agg.Aggregate,
|
|
|
|
"appID",
|
|
|
|
"name",
|
|
|
|
),
|
|
|
|
project.NewAPIConfigAddedEvent(ctx, &agg.Aggregate,
|
|
|
|
"appID",
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
"clientID@project",
|
2022-04-12 14:20:17 +00:00
|
|
|
nil,
|
|
|
|
domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
c := &Commands{
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
AssertValidation(t,
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
c.AddAPIAppCommand(
|
2022-04-12 14:20:17 +00:00
|
|
|
&addAPIApp{
|
|
|
|
AddApp: AddApp{
|
|
|
|
Aggregate: *tt.args.a,
|
|
|
|
ID: tt.args.appID,
|
|
|
|
Name: tt.args.name,
|
|
|
|
},
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
), tt.args.filter, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
func TestCommandSide_AddAPIApplication(t *testing.T) {
|
|
|
|
type fields struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
apiApp *domain.APIApp
|
|
|
|
resourceOwner string
|
|
|
|
secretGenerator crypto.Generator
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.APIApp
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no aggregate id, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "project not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsPreconditionFailed,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid app, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewProjectAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
2021-08-24 06:34:10 +00:00
|
|
|
"project", true, true, true,
|
|
|
|
domain.PrivateLabelingSettingUnspecified),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "",
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create api app basic, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewProjectAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
2021-08-24 06:34:10 +00:00
|
|
|
"project", true, true, true,
|
|
|
|
domain.PrivateLabelingSettingUnspecified),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewApplicationAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"app",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"client1@project",
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
domain.APIAuthMethodTypeBasic),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
uniqueConstraintsFromEventConstraint(project.NewAddApplicationUniqueConstraint("app", "project1")),
|
|
|
|
),
|
|
|
|
),
|
2022-02-16 15:49:17 +00:00
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "app1", "client1"),
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypeBasic,
|
|
|
|
},
|
2022-02-16 15:49:17 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
ClientID: "client1@project",
|
|
|
|
ClientSecretString: "a",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypeBasic,
|
|
|
|
State: domain.AppStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create api app jwt, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewProjectAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
2021-08-24 06:34:10 +00:00
|
|
|
"project", true, true, true,
|
|
|
|
domain.PrivateLabelingSettingUnspecified),
|
2021-03-15 11:51:15 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewApplicationAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"app",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"client1@project",
|
|
|
|
nil,
|
|
|
|
domain.APIAuthMethodTypePrivateKeyJWT),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
uniqueConstraintsFromEventConstraint(project.NewAddApplicationUniqueConstraint("app", "project1")),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "app1", "client1"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
ClientID: "client1@project",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
State: domain.AppStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
got, err := r.AddAPIApplication(tt.args.ctx, tt.args.apiApp, tt.args.resourceOwner, tt.args.secretGenerator)
|
2021-03-15 11:51:15 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_ChangeAPIApplication(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
apiApp *domain.APIApp
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.APIApp
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing appid, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "",
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing aggregateid, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "",
|
|
|
|
},
|
|
|
|
AppID: "appid",
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "app not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsNotFound,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no changes, precondition error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewApplicationAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"app",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"client1@project",
|
|
|
|
nil,
|
|
|
|
domain.APIAuthMethodTypePrivateKeyJWT),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsPreconditionFailed,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change api app, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewApplicationAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"app",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"client1@project",
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
domain.APIAuthMethodTypeBasic),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
newAPIAppChangedEvent(context.Background(),
|
|
|
|
"app1",
|
|
|
|
"project1",
|
|
|
|
"org1",
|
|
|
|
domain.APIAuthMethodTypePrivateKeyJWT),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
apiApp: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
ClientID: "client1@project",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
State: domain.AppStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := r.ChangeAPIApplication(tt.args.ctx, tt.args.apiApp, tt.args.resourceOwner)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_ChangeAPIApplicationSecret(t *testing.T) {
|
|
|
|
type fields struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore *eventstore.Eventstore
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
appID string
|
|
|
|
projectID string
|
|
|
|
resourceOwner string
|
|
|
|
secretGenerator crypto.Generator
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.APIApp
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no projectid, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
appID: "app1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no appid, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
projectID: "project1",
|
|
|
|
appID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsErrorInvalidArgument,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "app not existing, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
projectID: "project1",
|
|
|
|
appID: "app1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2022-04-12 14:20:17 +00:00
|
|
|
err: errors.IsNotFound,
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change secret, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewApplicationAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"app",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigAddedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
"client1@project",
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
domain.APIAuthMethodTypeBasic),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
[]*repository.Event{
|
|
|
|
eventFromEventPusher(
|
|
|
|
project.NewAPIConfigSecretChangedEvent(context.Background(),
|
|
|
|
&project.NewAggregate("project1", "org1").Aggregate,
|
|
|
|
"app1",
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
projectID: "project1",
|
|
|
|
appID: "app1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
2021-03-15 11:51:15 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.APIApp{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "project1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
AppID: "app1",
|
|
|
|
AppName: "app",
|
|
|
|
ClientID: "client1@project",
|
|
|
|
ClientSecretString: "a",
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypeBasic,
|
|
|
|
State: domain.AppStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore: tt.fields.eventstore,
|
2021-03-15 11:51:15 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
got, err := r.ChangeAPIApplicationSecret(tt.args.ctx, tt.args.projectID, tt.args.appID, tt.args.resourceOwner, tt.args.secretGenerator)
|
2021-03-15 11:51:15 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIAppChangedEvent(ctx context.Context, appID, projectID, resourceOwner string, authMethodType domain.APIAuthMethodType) *project.APIConfigChangedEvent {
|
|
|
|
changes := []project.APIConfigChanges{
|
|
|
|
project.ChangeAPIAuthMethodType(authMethodType),
|
|
|
|
}
|
|
|
|
event, _ := project.NewAPIConfigChangedEvent(ctx,
|
|
|
|
&project.NewAggregate(projectID, resourceOwner).Aggregate,
|
|
|
|
appID,
|
|
|
|
changes,
|
|
|
|
)
|
|
|
|
return event
|
|
|
|
}
|