Skip to content

Commit

Permalink
feat: change state handling of global configs that support patch upda…
Browse files Browse the repository at this point in the history
…tes and switched location type from ldap to db

Signed-off-by: moabu <47318409+moabu@users.noreply.github.com>
  • Loading branch information
moabu authored Apr 17, 2023
1 parent 5941bc5 commit 599c20f
Show file tree
Hide file tree
Showing 31 changed files with 400 additions and 184 deletions.
14 changes: 11 additions & 3 deletions terraform-provider-jans/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# Changelog

## 0.4.0 (2023-03-16)
## [0.5.0](https://github.com/JanssenProject/terraform-provider-jans/compare/v0.4.0...v0.5.0) (2023-04-17)


### Features

* change state handling of global configs that support patch updates
* switched location type from ldap to db

## [0.4.0](https://github.com/JanssenProject/terraform-provider-jans/compare/v0.3.0...v0.4.0) (2023-03-16)


### Features

* added data source for plugins

## 0.3.0 (2023-03-15)
## [0.3.0](https://github.com/JanssenProject/terraform-provider-jans/compare/v0.2.0...v0.3.0) (2023-03-15)


### Features

* added new resource for manaing api app config

## 0.2.0 (2023-02-16)
## [0.2.0](https://github.com/JanssenProject/terraform-provider-jans/compare/v0.1.0...v0.2.0) (2023-02-16)


### Features
Expand Down
1 change: 0 additions & 1 deletion terraform-provider-jans/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ If any of those 3 parameters is not provided, the provider will not be able to c
Optionally, users can also set the following variables:

* `insecure_client` - If set to `true`, the provider will not verify the TLS certificate of the Janssen server. This is useful for testing purposes and should not be used in production, unless absolutely unavoidable.

4 changes: 2 additions & 2 deletions terraform-provider-jans/docs/resources/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ resource "jans_script" "test" {
enabled = true
modified = false
internal = false
location_type = "LDAP"
location_type = "db"
base_dn = "inum=4A4E-4F3D,ou=scripts,o=jans"
module_properties {
value1 = "location_type"
value2 = "ldap"
value2 = "db"
}
module_properties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ resource "jans_script" "test" {
enabled = true
modified = false
internal = false
location_type = "LDAP"
location_type = "db"
base_dn = "inum=4A4E-4F3D,ou=scripts,o=jans"

module_properties {
value1 = "location_type"
value2 = "ldap"
value2 = "db"
}

module_properties {
Expand Down
18 changes: 9 additions & 9 deletions terraform-provider-jans/jans/api_app_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,34 @@ func (c *Client) GetApiAppConfiguration(ctx context.Context) (*ApiAppConfigurati
return &ret, nil
}

// UpdateApiAppConfiguration uses the provided api configuration to create a
// list of patch requests to update the Janssen api configuration properties.
func (c *Client) UpdateApiAppConfiguration(ctx context.Context, config *ApiAppConfiguration) (*ApiAppConfiguration, error) {
// PatchApiAppConfiguration uses the provided list of patch requests to update
// the Janssen api configuration properties.
func (c *Client) PatchApiAppConfiguration(ctx context.Context, patches []PatchRequest) (*ApiAppConfiguration, error) {

if config == nil {
return nil, fmt.Errorf("config is nil")
if len(patches) == 0 {
return c.GetApiAppConfiguration(ctx)
}

orig, err := c.GetApiAppConfiguration(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get app configuration: %w", err)
}

patches, err := createPatches(config, orig)
updates, err := createPatchesDiff(orig, patches)
if err != nil {
return nil, fmt.Errorf("failed to create patches: %w", err)
}

if len(patches) == 0 {
return nil, fmt.Errorf("no patches provided")
if len(updates) == 0 {
return c.GetApiAppConfiguration(ctx)
}

token, err := c.getToken(ctx, "https://jans.io/oauth/config/properties.write")
if err != nil {
return nil, fmt.Errorf("failed to get token: %w", err)
}

if err := c.patch(ctx, "/jans-config-api/api/v1/api-config", token, patches); err != nil {
if err := c.patch(ctx, "/jans-config-api/api/v1/api-config", token, updates); err != nil {
return nil, fmt.Errorf("patch request failed: %w", err)
}

Expand Down
14 changes: 10 additions & 4 deletions terraform-provider-jans/jans/api_app_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ func TestPatchApiAppConfig(t *testing.T) {

oldCount := cfg.MaxCount

cfg.MaxCount = 5
patches := []PatchRequest{
{
Op: "replace",
Path: "/maxCount",
Value: 5,
},
}

_, err = client.UpdateApiAppConfiguration(ctx, cfg)
_, err = client.PatchApiAppConfiguration(ctx, patches)
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
cfg.MaxCount = oldCount
_, _ = client.UpdateApiAppConfiguration(ctx, cfg)
patches[0].Value = oldCount
_, _ = client.PatchApiAppConfiguration(ctx, patches)
})

cfg, err = client.GetApiAppConfiguration(ctx)
Expand Down
18 changes: 9 additions & 9 deletions terraform-provider-jans/jans/app_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,34 +345,34 @@ func (c *Client) GetAppConfiguration(ctx context.Context) (*AppConfiguration, er
return ret, nil
}

// UpdateAuthServiceConfig uses the provided list of patch requests to update
// the Janssen authorization servcer application configuration properties.
func (c *Client) UpdateAppConfiguration(ctx context.Context, config *AppConfiguration) (*AppConfiguration, error) {
// PatchAppConfiguration uses the provided list of patch requests to update
// the Janssen authorization server application configuration properties.
func (c *Client) PatchAppConfiguration(ctx context.Context, patches []PatchRequest) (*AppConfiguration, error) {

if config == nil {
return nil, fmt.Errorf("config is nil")
if len(patches) == 0 {
return c.GetAppConfiguration(ctx)
}

orig, err := c.GetAppConfiguration(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get app configuration: %w", err)
}

patches, err := createPatches(config, orig)
updates, err := createPatchesDiff(orig, patches)
if err != nil {
return nil, fmt.Errorf("failed to create patches: %w", err)
}

if len(patches) == 0 {
return nil, fmt.Errorf("no patches provided")
if len(updates) == 0 {
return c.GetAppConfiguration(ctx)
}

token, err := c.getToken(ctx, "https://jans.io/oauth/jans-auth-server/config/properties.write")
if err != nil {
return nil, fmt.Errorf("failed to get token: %w", err)
}

if err := c.patch(ctx, "/jans-config-api/api/v1/jans-auth-server/config", token, patches); err != nil {
if err := c.patch(ctx, "/jans-config-api/api/v1/jans-auth-server/config", token, updates); err != nil {
return nil, fmt.Errorf("patch request failed: %w", err)
}

Expand Down
16 changes: 12 additions & 4 deletions terraform-provider-jans/jans/app_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ func TestPatchAuthConfig(t *testing.T) {

rand.Seed(time.Now().UnixNano())

newEntry := fmt.Sprintf("*.attacker-%v.com/*", rand.Intn(100))
newEntry := []string{fmt.Sprintf("*.attacker-%v.com/*", rand.Intn(100))}

patches := []PatchRequest{
{
Op: "replace",
Path: "/clientBlackList",
Value: newEntry,
},
}

cfg.ClientBlackList = []string{newEntry}
cfg.ClientBlackList = newEntry

if _, err := client.UpdateAppConfiguration(ctx, cfg); err != nil {
if _, err := client.PatchAppConfiguration(ctx, patches); err != nil {
t.Fatal(err)
}

Expand All @@ -61,7 +69,7 @@ func TestPatchAuthConfig(t *testing.T) {
t.Fatal("expected 1 client in blacklist")
}

if (cfg.ClientBlackList[0]) != newEntry {
if (cfg.ClientBlackList[0]) != newEntry[0] {
t.Fatalf("expected '%s' in blacklist, got '%s'", newEntry, cfg.ClientBlackList[0])
}

Expand Down
28 changes: 14 additions & 14 deletions terraform-provider-jans/jans/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,36 @@ func (c *Client) GetCacheConfiguration(ctx context.Context) (*CacheConfiguration
return ret, nil
}

// UpdateCacheConfiguration peforms partial modifications of the cache
// configuration.
func (c *Client) UpdateCacheConfiguration(ctx context.Context, config *CacheConfiguration) error {
// PatchCacheConfiguration uses the provided list of patch requests to update
// the cache configuration.
func (c *Client) PatchCacheConfiguration(ctx context.Context, patches []PatchRequest) (*CacheConfiguration, error) {

if config == nil {
return fmt.Errorf("config is nil")
if len(patches) == 0 {
return c.GetCacheConfiguration(ctx)
}

orig, err := c.GetCacheConfiguration(ctx)
if err != nil {
return fmt.Errorf("failed to get cache configuration: %w", err)
return nil, fmt.Errorf("failed to get cache configuration: %w", err)
}

token, err := c.getToken(ctx, "https://jans.io/oauth/config/cache.write")
if err != nil {
return fmt.Errorf("failed to get token: %w", err)
return nil, fmt.Errorf("failed to get token: %w", err)
}

patches, err := createPatches(config, orig)
updates, err := createPatchesDiff(orig, patches)
if err != nil {
return fmt.Errorf("failed to create patches: %w", err)
return nil, fmt.Errorf("failed to create patches: %w", err)
}

if len(patches) == 0 {
return fmt.Errorf("no patches created")
if len(updates) == 0 {
return c.GetCacheConfiguration(ctx)
}

if err := c.patch(ctx, "/jans-config-api/api/v1/config/cache", token, patches); err != nil {
return fmt.Errorf("patch request failed: %w", err)
if err := c.patch(ctx, "/jans-config-api/api/v1/config/cache", token, updates); err != nil {
return nil, fmt.Errorf("patch request failed: %w", err)
}

return nil
return c.GetCacheConfiguration(ctx)
}
Loading

0 comments on commit 599c20f

Please sign in to comment.