-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add custom certificate #983
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
Open
matheuspolitano
wants to merge
16
commits into
stackitcloud:main
Choose a base branch
from
matheuspolitano:mp/cdn-custom-domain-add-byoc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
de9b3cf
add custom certificate
9810b13
remove print
ce9c3e0
fix error test
f59e745
Adjust certificate
7cec35c
format resource
9628c9c
Merge branch 'main' into mp/cdn-custom-domain-add-byoc
matheuspolitano 72bc88f
Update stackit/internal/services/cdn/customdomain/resource.go
matheuspolitano e64df73
Update stackit/internal/services/cdn/customdomain/resource.go
matheuspolitano 67b990d
Update stackit/internal/services/cdn/customdomain/resource.go
matheuspolitano f01ded0
Update stackit/internal/services/cdn/customdomain/datasource.go
matheuspolitano c4b81a4
improve code
510b0e7
Update stackit/internal/services/cdn/customdomain/resource.go
matheuspolitano e7be669
add update certificate
b8162cd
remove dialogs
6ea7aa7
Update stackit/internal/services/cdn/customdomain/resource_test.go
matheuspolitano e56d3cb
apply code suggestion
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,13 @@ package cdn_test | |
|
||
import ( | ||
"context" | ||
cryptoRand "crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"net" | ||
"strings" | ||
"testing" | ||
|
@@ -51,7 +57,7 @@ func configResources(regions string) string { | |
resource "stackit_dns_zone" "dns_zone" { | ||
project_id = "%s" | ||
name = "cdn_acc_test_zone" | ||
dns_name = "cdntestzone.stackit.gg" | ||
dns_name = "cdntest-zone.stackit.gg" | ||
contact_email = "[email protected]" | ||
type = "primary" | ||
default_ttl = 3600 | ||
|
@@ -68,19 +74,23 @@ func configResources(regions string) string { | |
testutil.ProjectId, instanceResource["custom_domain_prefix"]) | ||
} | ||
|
||
func configCustomDomainResources(regions string) string { | ||
func configCustomDomainResources(regions, cert, key string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
resource "stackit_cdn_custom_domain" "custom_domain" { | ||
project_id = stackit_cdn_distribution.distribution.project_id | ||
distribution_id = stackit_cdn_distribution.distribution.distribution_id | ||
name = "${stackit_dns_record_set.dns_record.name}.cdntestzone.stackit.gg" | ||
name = "${stackit_dns_record_set.dns_record.name}.cdntest-zone.stackit.gg" | ||
certificate = { | ||
certificate = %q | ||
private_key = %q | ||
} | ||
} | ||
`, configResources(regions)) | ||
`, configResources(regions), cert, key) | ||
} | ||
|
||
func configDatasources(regions string) string { | ||
func configDatasources(regions, cert, key string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
|
@@ -93,11 +103,54 @@ func configDatasources(regions string) string { | |
project_id = stackit_cdn_custom_domain.custom_domain.project_id | ||
distribution_id = stackit_cdn_custom_domain.custom_domain.distribution_id | ||
name = stackit_cdn_custom_domain.custom_domain.name | ||
|
||
} | ||
`, configCustomDomainResources(regions)) | ||
`, configCustomDomainResources(regions, cert, key)) | ||
} | ||
|
||
func makeCertAndKey(t *testing.T, organization string) (cert, key []byte) { | ||
privateKey, err := rsa.GenerateKey(cryptoRand.Reader, 2048) | ||
if err != nil { | ||
t.Fatalf("failed to generate key: %s", err.Error()) | ||
} | ||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(1), | ||
Issuer: pkix.Name{CommonName: organization}, | ||
Subject: pkix.Name{ | ||
Organization: []string{organization}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(time.Hour), | ||
|
||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
cert, err = x509.CreateCertificate( | ||
cryptoRand.Reader, | ||
&template, | ||
&template, | ||
&privateKey.PublicKey, | ||
privateKey, | ||
) | ||
if err != nil { | ||
t.Fatalf("failed to generate cert: %s", err.Error()) | ||
} | ||
|
||
return pem.EncodeToMemory(&pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: cert, | ||
}), pem.EncodeToMemory(&pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey), | ||
}) | ||
} | ||
func TestAccCDNDistributionResource(t *testing.T) { | ||
organization := fmt.Sprintf("organization-%s", uuid.NewString()) | ||
cert, key := makeCertAndKey(t, organization) | ||
|
||
organization_updated := fmt.Sprintf("organization-updated-%s", uuid.NewString()) | ||
cert_updated, key_updated := makeCertAndKey(t, organization_updated) | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, | ||
CheckDestroy: testAccCheckCDNDistributionDestroy, | ||
|
@@ -128,16 +181,16 @@ func TestAccCDNDistributionResource(t *testing.T) { | |
{ | ||
Config: configResources(instanceResource["config_regions"]), | ||
Check: func(_ *terraform.State) error { | ||
_, err := blockUntilDomainResolves(instanceResource["custom_domain_prefix"] + ".cdntestzone.stackit.gg") | ||
_, err := blockUntilDomainResolves(instanceResource["custom_domain_prefix"] + ".cdntest-zone.stackit.gg") | ||
return err | ||
}, | ||
}, | ||
// Custom Domain Create | ||
{ | ||
Config: configCustomDomainResources(instanceResource["config_regions"]), | ||
Config: configCustomDomainResources(instanceResource["config_regions"], string(cert), string(key)), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntestzone.stackit.gg"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntest-zone.stackit.gg"), | ||
resource.TestCheckResourceAttrPair("stackit_cdn_distribution.distribution", "distribution_id", "stackit_cdn_custom_domain.custom_domain", "distribution_id"), | ||
resource.TestCheckResourceAttrPair("stackit_cdn_distribution.distribution", "project_id", "stackit_cdn_custom_domain.custom_domain", "project_id"), | ||
), | ||
|
@@ -181,17 +234,21 @@ func TestAccCDNDistributionResource(t *testing.T) { | |
}, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{ | ||
"certificate.certificate", | ||
"certificate.private_key", | ||
}, | ||
}, | ||
// Data Source | ||
{ | ||
Config: configDatasources(instanceResource["config_regions"]), | ||
Config: configDatasources(instanceResource["config_regions"], string(cert), string(key)), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.stackit_cdn_distribution.distribution", "distribution_id"), | ||
resource.TestCheckResourceAttrSet("data.stackit_cdn_distribution.distribution", "created_at"), | ||
resource.TestCheckResourceAttrSet("data.stackit_cdn_distribution.distribution", "updated_at"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.#", "2"), | ||
resource.TestCheckResourceAttrSet("data.stackit_cdn_distribution.distribution", "domains.0.name"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.1.name", instanceResource["custom_domain_prefix"]+".cdntestzone.stackit.gg"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.1.name", instanceResource["custom_domain_prefix"]+".cdntest-zone.stackit.gg"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.0.status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.1.status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "domains.0.type", "managed"), | ||
|
@@ -206,20 +263,21 @@ func TestAccCDNDistributionResource(t *testing.T) { | |
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "project_id", testutil.ProjectId), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_custom_domain.custom_domain", "status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntestzone.stackit.gg"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_custom_domain.custom_domain", "certificate.version", "1"), | ||
resource.TestCheckResourceAttr("data.stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntest-zone.stackit.gg"), | ||
resource.TestCheckResourceAttrPair("stackit_cdn_distribution.distribution", "distribution_id", "stackit_cdn_custom_domain.custom_domain", "distribution_id"), | ||
), | ||
}, | ||
// Update | ||
{ | ||
Config: configCustomDomainResources(instanceResource["config_regions_updated"]), | ||
Config: configCustomDomainResources(instanceResource["config_regions_updated"], string(cert_updated), string(key_updated)), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("stackit_cdn_distribution.distribution", "distribution_id"), | ||
resource.TestCheckResourceAttrSet("stackit_cdn_distribution.distribution", "created_at"), | ||
resource.TestCheckResourceAttrSet("stackit_cdn_distribution.distribution", "updated_at"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.#", "2"), | ||
resource.TestCheckResourceAttrSet("stackit_cdn_distribution.distribution", "domains.0.name"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.1.name", instanceResource["custom_domain_prefix"]+".cdntestzone.stackit.gg"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.1.name", instanceResource["custom_domain_prefix"]+".cdntest-zone.stackit.gg"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.0.status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.1.status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.0.type", "managed"), | ||
|
@@ -235,7 +293,8 @@ func TestAccCDNDistributionResource(t *testing.T) { | |
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "project_id", testutil.ProjectId), | ||
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "status", "ACTIVE"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntestzone.stackit.gg"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "certificate.version", "2"), | ||
resource.TestCheckResourceAttr("stackit_cdn_custom_domain.custom_domain", "name", instanceResource["custom_domain_prefix"]+".cdntest-zone.stackit.gg"), | ||
resource.TestCheckResourceAttrPair("stackit_cdn_distribution.distribution", "distribution_id", "stackit_cdn_custom_domain.custom_domain", "distribution_id"), | ||
resource.TestCheckResourceAttrPair("stackit_cdn_distribution.distribution", "project_id", "stackit_cdn_custom_domain.custom_domain", "project_id"), | ||
), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run the acc tests, I get an error that this dns_name is already used. To avoid collisions, this should be (partly) a random name. You can create a var, which stores the dns name for the acc test run and reuse it everywhere. e.g.: