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

feat: add all supported alogithms for rsa-enc keystore #1092

Open
wants to merge 2 commits into
base: main
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
3 changes: 2 additions & 1 deletion provider/resource_keycloak_realm_keystore_java_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/keycloak/terraform-provider-keycloak/keycloak"
)

var (
keycloakRealmKeystoreJavaKeystoreAlgorithm = []string{"RS256", "RS384", "RS512", "PS256", "PS384", "PS512"}
keycloakRealmKeystoreJavaKeystoreAlgorithm = []string{"AES", "EdDSA", "ES256", "ES384", "ES512", "HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "RSA1_5", "RSA-OAEP", "RSA-OAEP-256", "ECDH-ES", "ECDH-ES+A128KW", "ECDH-ES+A192KW", "ECDH-ES+A256KW"}
)

func resourceKeycloakRealmKeystoreJavaKeystore() *schema.Resource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package provider

import (
"fmt"
"regexp"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/keycloak/terraform-provider-keycloak/keycloak"
"regexp"
"strconv"
"testing"
)

func TestAccKeycloakRealmKeystoreJava_basic(t *testing.T) {
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestAccKeycloakRealmKeystoreJava_algorithmValidation(t *testing.T) {

skipIfEnvSet(t, "CI") // temporary while I figure out how to put java keystore file to keycloak container in CI

algorithm := randomStringInSlice(keycloakRealmKeystoreRsaAlgorithm)
algorithm := randomStringInSlice(keycloakRealmKeystoreJavaKeystoreAlgorithm)

resource.Test(t, resource.TestCase{
ProviderFactories: testAccProviderFactories,
Expand Down
6 changes: 4 additions & 2 deletions provider/resource_keycloak_realm_keystore_rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/keycloak/terraform-provider-keycloak/keycloak"
)

var (
keycloakRealmKeystoreRsaAlgorithm = []string{"RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "RSA-OAEP"}
keycloakRealmKeystoreRsaAlgorithm = []string{"RS256", "RS384", "RS512", "PS256", "PS384", "PS512"}
keycloakRealmKeystoreRsaEncAlgorithm = []string{"RSA1_5", "RSA-OAEP", "RSA-OAEP-256"}
)

func resourceKeycloakRealmKeystoreRsa() *schema.Resource {
Expand Down Expand Up @@ -53,7 +55,7 @@ func resourceKeycloakRealmKeystoreRsa() *schema.Resource {
"algorithm": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(keycloakRealmKeystoreRsaAlgorithm, false),
ValidateFunc: validation.StringInSlice(append(keycloakRealmKeystoreRsaAlgorithm, keycloakRealmKeystoreRsaEncAlgorithm...), false),
Default: "RS256",
Description: "Intended algorithm for the key",
},
Expand Down
32 changes: 22 additions & 10 deletions provider/resource_keycloak_realm_keystore_rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/keycloak/terraform-provider-keycloak/keycloak"
"log"
"math/big"
"regexp"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/keycloak/terraform-provider-keycloak/keycloak"
)

func TestAccKeycloakRealmKeystoreRsa_basic(t *testing.T) {
Expand Down Expand Up @@ -78,7 +79,8 @@ func TestAccKeycloakRealmKeystoreRsa_createAfterManualDestroy(t *testing.T) {
func TestAccKeycloakRealmKeystoreRsa_algorithmValidation(t *testing.T) {
t.Parallel()

algorithm := randomStringInSlice(keycloakRealmKeystoreRsaAlgorithm)
rsaAlgorithm := randomStringInSlice(keycloakRealmKeystoreRsaAlgorithm)
rsaEncAlgorithm := randomStringInSlice(keycloakRealmKeystoreRsaEncAlgorithm)
privateKey, certificate := generateKeyAndCert(2048)

resource.Test(t, resource.TestCase{
Expand All @@ -87,12 +89,22 @@ func TestAccKeycloakRealmKeystoreRsa_algorithmValidation(t *testing.T) {
CheckDestroy: testAccCheckRealmKeystoreRsaDestroy(),
Steps: []resource.TestStep{
{
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation(algorithm, "algorithm",
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation("rsa", rsaAlgorithm, "algorithm",
acctest.RandString(10), privateKey, certificate),
ExpectError: regexp.MustCompile("expected algorithm to be one of .+ got .+"),
},
{
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation(algorithm, "algorithm", algorithm,
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation("rsa", rsaAlgorithm, "algorithm", rsaAlgorithm,
privateKey, certificate),
Check: testAccCheckRealmKeystoreRsaExists("keycloak_realm_keystore_rsa.realm_rsa"),
},
{
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation("rsa-enc", rsaEncAlgorithm, "algorithm",
acctest.RandString(10), privateKey, certificate),
ExpectError: regexp.MustCompile("expected algorithm to be one of .+ got .+"),
},
{
Config: testKeycloakRealmKeystoreRsa_basicWithAttrValidation("rsa-enc", rsaEncAlgorithm, "algorithm", rsaEncAlgorithm,
privateKey, certificate),
Check: testAccCheckRealmKeystoreRsaExists("keycloak_realm_keystore_rsa.realm_rsa"),
},
Expand Down Expand Up @@ -216,7 +228,6 @@ data "keycloak_realm" "realm" {
}

resource "keycloak_realm_keystore_rsa" "realm_rsa" {

name = "%s"
realm_id = data.keycloak_realm.realm.id

Expand All @@ -228,7 +239,7 @@ resource "keycloak_realm_keystore_rsa" "realm_rsa" {
`, testAccRealmUserFederation.Realm, rsaName, privateKey, certificate)
}

func testKeycloakRealmKeystoreRsa_basicWithAttrValidation(rsaName, attr, val, privateKey,
func testKeycloakRealmKeystoreRsa_basicWithAttrValidation(provider, rsaName, attr, val, privateKey,
certificate string) string {
return fmt.Sprintf(`
data "keycloak_realm" "realm" {
Expand All @@ -243,6 +254,7 @@ resource "keycloak_realm_keystore_rsa" "realm_rsa" {

private_key = "%s"
certificate = "%s"
provider_id = "%s"
}
`, testAccRealmUserFederation.Realm, rsaName, attr, val, privateKey, certificate)
`, testAccRealmUserFederation.Realm, rsaName, attr, val, privateKey, certificate, provider)
}
Loading