Skip to content

Commit

Permalink
Merge pull request #58 from jfrog/fix-provider-crash
Browse files Browse the repository at this point in the history
Fix provider crash
  • Loading branch information
alexhung authored Apr 15, 2024
2 parents 1efb2b4 + a7bace8 commit 81093f8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.7.1 (Apr 15, 2024)

BUG FIXES:

* provider: Fix crashes when `url` attribute is used to set JFrog platform URL (vs env var). Issue: [#57](https://github.com/jfrog/terraform-provider-platform/issues/57) PR: [#58](https://github.com/jfrog/terraform-provider-platform/pull/58)

## 1.7.0 (Apr 12, 2024)

FEATURES:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.22.1
github.com/hashicorp/terraform-plugin-testing v1.7.0
github.com/jfrog/terraform-provider-shared v1.23.0
github.com/jfrog/terraform-provider-shared v1.24.0
github.com/samber/lo v1.39.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jfrog/terraform-provider-shared v1.23.0 h1:O8yKQMFrinHwt9CUxZag6TwiXMlajIFO66CAxS6VKqk=
github.com/jfrog/terraform-provider-shared v1.23.0/go.mod h1:OozwvfahZU4Q9u3kXdpQI3h/Etrs2DXoouQDrpxB1cQ=
github.com/jfrog/terraform-provider-shared v1.24.0 h1:VItpElBn9Jku8gtN7DhOURUIyoUYYw8R9erPBPgKwv8=
github.com/jfrog/terraform-provider-shared v1.24.0/go.mod h1:OozwvfahZU4Q9u3kXdpQI3h/Etrs2DXoouQDrpxB1cQ=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
33 changes: 19 additions & 14 deletions pkg/platform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var productId = "terraform-provider-platform/" + Version
var _ provider.Provider = (*PlatformProvider)(nil)

type PlatformProviderMetadata struct {
util.ProvderMetadata
util.ProviderMetadata
MyJFrogClient *resty.Client
}

Expand Down Expand Up @@ -61,12 +61,25 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
return
}

if config.Url.ValueString() != "" {
url = config.Url.ValueString()
}

if url == "" {
resp.Diagnostics.AddError(
"Missing URL Configuration",
"While configuring the provider, the url was not found in the JFROG_URL environment variable or provider configuration block url attribute.",
)
return
}

platformClient, err := client.Build(url, productId)
if err != nil {
resp.Diagnostics.AddError(
"Error creating Resty client",
err.Error(),
)
return
}

oidcAccessToken, err := util.OIDCTokenExchange(ctx, platformClient, config.OIDCProviderName.ValueString())
Expand All @@ -75,6 +88,7 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
"Failed OIDC ID token exchange",
err.Error(),
)
return
}

// use token from OIDC provider, which should take precedence over
Expand All @@ -101,18 +115,6 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
myJFrogAPIToken = config.MyJFrogAPIToken.ValueString()
}

if config.Url.ValueString() != "" {
url = config.Url.ValueString()
}

if url == "" {
resp.Diagnostics.AddError(
"Missing URL Configuration",
"While configuring the provider, the url was not found in the JFROG_URL environment variable or provider configuration block url attribute.",
)
return
}

var myJFrogClient *resty.Client
if len(myJFrogAPIToken) > 0 {
c, err := client.Build("https://my.jfrog.com", productId)
Expand All @@ -121,6 +123,7 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
"Error creating Resty client for MyJFrog",
err.Error(),
)
return
}

c, err = client.AddAuth(c, "", myJFrogAPIToken)
Expand All @@ -129,6 +132,7 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
"Error adding Auth to Resty client for MyJFrog",
err.Error(),
)
return
}

myJFrogClient = c
Expand All @@ -140,6 +144,7 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
"Error adding Auth to Resty client",
err.Error(),
)
return
}

if config.CheckLicense.IsNull() || config.CheckLicense.ValueBool() {
Expand All @@ -165,7 +170,7 @@ func (p *PlatformProvider) Configure(ctx context.Context, req provider.Configure
util.SendUsage(ctx, platformClient, productId, featureUsage)

meta := PlatformProviderMetadata{
ProvderMetadata: util.ProvderMetadata{
ProviderMetadata: util.ProviderMetadata{
Client: platformClient,
ArtifactoryVersion: version,
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/platform/resource_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccPermission_full(t *testing.T) {
resource "artifactory_managed_user" "{{ .userName }}" {
name = "{{ .userName }}"
email = "{{ .userName }}@tempurl.org"
password = "Password!123"
}
resource "artifactory_group" "{{ .groupName }}" {
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestAccPermission_full(t *testing.T) {
resource "artifactory_managed_user" "{{ .userName }}" {
name = "{{ .userName }}"
email = "{{ .userName }}@tempurl.org"
password = "Password!123"
}
resource "artifactory_group" "{{ .groupName }}" {
Expand Down

0 comments on commit 81093f8

Please sign in to comment.