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

Fix various webhook resource crashes #973

Merged
merged 3 commits into from
May 23, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 10.8.1 (May 24, 2024)

BUG FIXES:

* resource/artifactory_\*\_webhook, resource/artifactory_\*\_custom_webhook: Fix various crashes when importing the resource with optional attributes not set in the configuration. PR: [#973](https://github.com/jfrog/terraform-provider-artifactory/pull/973)

## 10.8.0 (May 20, 2024)

IMPROVEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ terraform {
}
}

provider "platform" {
provider "artifactory" {
url = "https://myinstance.jfrog.io"
oidc_provider_name = "terraform-cloud"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
webhookHandler := CustomHandler{
HandlerType: "custom-webhook",
Url: h["url"].(string),
Secrets: unpackKeyValuePair(h["secrets"].(map[string]interface{})),
Proxy: h["proxy"].(string),
HttpHeaders: unpackKeyValuePair(h["http_headers"].(map[string]interface{})),
Payload: h["payload"].(string),
}

if v, ok := h["secrets"]; ok {
webhookHandler.Secrets = unpackKeyValuePair(v.(map[string]interface{}))
}

if v, ok := h["proxy"]; ok {
webhookHandler.Proxy = v.(string)
}

if v, ok := h["http_headers"]; ok {
webhookHandler.HttpHeaders = unpackKeyValuePair(v.(map[string]interface{}))
}

if v, ok := h["payload"]; ok {
webhookHandler.Payload = v.(string)
}

webhookHandlers = append(webhookHandlers, webhookHandler)
}
}
Expand Down Expand Up @@ -208,12 +221,19 @@ func ResourceArtifactoryCustomWebhook(webhookType string) *schema.Resource {
packedHandlers := make([]interface{}, len(handlers))
for _, handler := range handlers {
packedHandler := map[string]interface{}{
"url": handler.Url,
"secrets": packSecretsCustom(handler.Secrets, d, handler.Url),
"proxy": handler.Proxy,
"http_headers": packKeyValuePair(handler.HttpHeaders),
"payload": handler.Payload,
"url": handler.Url,
"proxy": handler.Proxy,
"payload": handler.Payload,
}

if handler.Secrets != nil {
packedHandler["secrets"] = packSecretsCustom(handler.Secrets, d, handler.Url)
}

if handler.HttpHeaders != nil {
packedHandler["http_headers"] = packKeyValuePair(handler.HttpHeaders)
}

packedHandlers = append(packedHandlers, packedHandler)
}

Expand Down
58 changes: 42 additions & 16 deletions pkg/artifactory/resource/webhook/resource_artifactory_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ const WhUrl = webhooksUrl + "/{webhookKey}"
const currentSchemaVersion = 2

var unpackKeyValuePair = func(keyValuePairs map[string]interface{}) []KeyValuePair {
var KVPairs []KeyValuePair
var kvPairs []KeyValuePair
for key, value := range keyValuePairs {
keyValuePair := KeyValuePair{
Name: key,
Value: value.(string),
}
KVPairs = append(KVPairs, keyValuePair)
kvPairs = append(kvPairs, keyValuePair)
}

return KVPairs
return kvPairs
}

var packKeyValuePair = func(keyValuePairs []KeyValuePair) map[string]interface{} {
KVPairs := make(map[string]interface{})
kvPairs := make(map[string]interface{})
for _, keyValuePair := range keyValuePairs {
KVPairs[keyValuePair.Name] = keyValuePair.Value
kvPairs[keyValuePair.Name] = keyValuePair.Value
}

return KVPairs
return kvPairs
}

var domainCriteriaLookup = map[string]interface{}{
Expand Down Expand Up @@ -166,8 +166,17 @@ var packCriteria = func(d *schema.ResourceData, webhookType string, criteria map
resource := domainSchemaLookup(currentSchemaVersion, false, webhookType)[webhookType]["criteria"].Elem.(*schema.Resource)
packedCriteria := domainPackLookup[webhookType](criteria)

packedCriteria["include_patterns"] = schema.NewSet(schema.HashString, criteria["includePatterns"].([]interface{}))
packedCriteria["exclude_patterns"] = schema.NewSet(schema.HashString, criteria["excludePatterns"].([]interface{}))
includePatterns := []interface{}{}
if v, ok := criteria["includePatterns"]; ok && v != nil {
includePatterns = v.([]interface{})
}
packedCriteria["include_patterns"] = schema.NewSet(schema.HashString, includePatterns)

excludePatterns := []interface{}{}
if v, ok := criteria["excludePatterns"]; ok && v != nil {
excludePatterns = v.([]interface{})
}
packedCriteria["exclude_patterns"] = schema.NewSet(schema.HashString, excludePatterns)

return setValue("criteria", schema.NewSet(schema.HashResource(resource), []interface{}{packedCriteria}))
}
Expand All @@ -190,7 +199,7 @@ var packSecret = func(d *schema.ResourceData, url string) string {
for _, handler := range handlers {
h := handler.(map[string]interface{})
// if urls match, assign the secret value from the state
if h["url"] == url {
if h["url"].(string) == url {
secret = h["secret"].(string)
}
}
Expand All @@ -215,13 +224,26 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
// https://discuss.hashicorp.com/t/using-typeset-in-provider-always-adds-an-empty-element-on-update/18566/2
if h["url"].(string) != "" {
webhookHandler := Handler{
HandlerType: "webhook",
Url: h["url"].(string),
Secret: h["secret"].(string),
UseSecretForSigning: h["use_secret_for_signing"].(bool),
Proxy: h["proxy"].(string),
CustomHttpHeaders: unpackKeyValuePair(h["custom_http_headers"].(map[string]interface{})),
HandlerType: "webhook",
Url: h["url"].(string),
}

if v, ok := h["secret"]; ok {
webhookHandler.Secret = v.(string)
}

if v, ok := h["use_secret_for_signing"]; ok {
webhookHandler.UseSecretForSigning = v.(bool)
}

if v, ok := h["proxy"]; ok {
webhookHandler.Proxy = v.(string)
}

if v, ok := h["custom_http_headers"]; ok {
webhookHandler.CustomHttpHeaders = unpackKeyValuePair(v.(map[string]interface{}))
}

webhookHandlers = append(webhookHandlers, webhookHandler)
}
}
Expand Down Expand Up @@ -255,8 +277,12 @@ func ResourceArtifactoryWebhook(webhookType string) *schema.Resource {
"secret": packSecret(d, handler.Url),
"use_secret_for_signing": handler.UseSecretForSigning,
"proxy": handler.Proxy,
"custom_http_headers": packKeyValuePair(handler.CustomHttpHeaders),
}

if handler.CustomHttpHeaders != nil {
packedHandler["custom_http_headers"] = packKeyValuePair(handler.CustomHttpHeaders)
}

packedHandlers = append(packedHandlers, packedHandler)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ var repoWebhookSchema = func(webhookType string, version int, isCustom bool) map
}

var packRepoCriteria = func(artifactoryCriteria map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
criteria := map[string]interface{}{
"any_local": artifactoryCriteria["anyLocal"].(bool),
"any_remote": artifactoryCriteria["anyRemote"].(bool),
"any_federated": artifactoryCriteria["anyFederated"].(bool),
"any_federated": false,
"repo_keys": schema.NewSet(schema.HashString, artifactoryCriteria["repoKeys"].([]interface{})),
}

if v, ok := artifactoryCriteria["anyFederated"]; ok {
criteria["any_federated"] = v.(bool)
}

return criteria
}

var unpackRepoCriteria = func(terraformCriteria map[string]interface{}, baseCriteria BaseWebhookCriteria) interface{} {
Expand Down
Loading
Loading