Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix consumer rsa public key must be nullable #153

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions kong/resource_kong_consumer_jwt_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package kong
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/kong/go-kong/kong"
"strings"
)

func resourceKongConsumerJWTAuth() *schema.Resource {
Expand Down Expand Up @@ -37,7 +38,7 @@ func resourceKongConsumerJWTAuth() *schema.Resource {
},
"rsa_public_key": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: false,
},
"secret": {
Expand All @@ -58,10 +59,10 @@ func resourceKongConsumerJWTAuth() *schema.Resource {
func resourceKongConsumerJWTAuthCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

JWTAuthRequest := &kong.JWTAuth{
Algorithm: kong.String(d.Get("algorithm").(string)),
Key: kong.String(d.Get("key").(string)),
RSAPublicKey: kong.String(d.Get("rsa_public_key").(string)),
Secret: kong.String(d.Get("secret").(string)),
Algorithm: readStringPtrFromResource(d, "algorithm"),
Key: readStringPtrFromResource(d, "key"),
RSAPublicKey: readStringPtrFromResource(d, "rsa_public_key"),
Secret: readStringPtrFromResource(d, "secret"),
Tags: readStringArrayPtrFromResource(d, "tags"),
}

Expand Down Expand Up @@ -103,10 +104,10 @@ func resourceKongConsumerJWTAuthUpdate(ctx context.Context, d *schema.ResourceDa

JWTAuthRequest := &kong.JWTAuth{
ID: kong.String(id.ID),
Algorithm: kong.String(d.Get("algorithm").(string)),
Key: kong.String(d.Get("key").(string)),
RSAPublicKey: kong.String(d.Get("rsa_public_key").(string)),
Secret: kong.String(d.Get("secret").(string)),
Algorithm: readStringPtrFromResource(d, "algorithm"),
Key: readStringPtrFromResource(d, "key"),
RSAPublicKey: readStringPtrFromResource(d, "rsa_public_key"),
Secret: readStringPtrFromResource(d, "secret"),
Tags: readStringArrayPtrFromResource(d, "tags"),
}

Expand Down
49 changes: 48 additions & 1 deletion kong/resource_kong_consumer_jwt_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package kong
import (
"context"
"fmt"
"github.com/kong/go-kong/kong"
"regexp"
"testing"

"github.com/kong/go-kong/kong"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -41,6 +43,28 @@ func TestAccJWTAuth(t *testing.T) {
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config", "tags.0", "foo"),
),
},
{
Config: testCreateJWTAuthConfigDefaults,
Check: resource.ComposeTestCheckFunc(
testAccCheckJWTAuthExists("kong_consumer_jwt_auth.consumer_jwt_config_defaults"),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "algorithm", "HS256"),
resource.TestMatchResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "key", regexp.MustCompile(".+")),
resource.TestMatchResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "secret", regexp.MustCompile(".+")),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "rsa_public_key", ""),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "tags.#", "0"),
),
ExpectNonEmptyPlan: true,
},
{
Config: testCreateJWTAuthConfigDefaultsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckJWTAuthExists("kong_consumer_jwt_auth.consumer_jwt_config_defaults"),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "algorithm", "HS256"),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "key", "updated_key"),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "secret", "updated_secret"),
resource.TestCheckResourceAttr("kong_consumer_jwt_auth.consumer_jwt_config_defaults", "tags.#", "0"),
),
},
},
})
}
Expand Down Expand Up @@ -165,3 +189,26 @@ resource "kong_consumer_jwt_auth" "consumer_jwt_config" {
tags = ["foo"]
}
`
const testCreateJWTAuthConfigDefaults = `
resource "kong_consumer" "consumer_jwt_defaults" {
username = "consumer_jwt"
custom_id = "666"
}

resource "kong_consumer_jwt_auth" "consumer_jwt_config_defaults" {
consumer_id = "${kong_consumer.consumer_jwt_defaults.id}"
}
`
const testCreateJWTAuthConfigDefaultsUpdate = `
resource "kong_consumer" "consumer_jwt_defaults" {
username = "consumer_jwt"
custom_id = "666"
}

resource "kong_consumer_jwt_auth" "consumer_jwt_config_defaults" {
consumer_id = "${kong_consumer.consumer_jwt_defaults.id}"
algorithm = "HS256"
key = "updated_key"
secret = "updated_secret"
}
`