Skip to content

Commit

Permalink
Merge pull request #125 from jfrog/GH-121-fix-panic
Browse files Browse the repository at this point in the history
GH-121 - casting exeception fixed
  • Loading branch information
chb0github authored Sep 9, 2021
2 parents f9b3f0a + 3e87184 commit 43214b8
Show file tree
Hide file tree
Showing 9 changed files with 698 additions and 124 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/jfrog/terraform-provider-artifactory
require (
github.com/go-resty/resty/v2 v2.4.0
github.com/google/go-querystring v1.0.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/jfrog/jfrog-client-go v0.23.1
Expand Down
8 changes: 8 additions & 0 deletions http/http-client.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"localhost" : {
"host" : "http://localhost:9000"
},
"partnership": {
"host" : "https://partnership.jfrog.io"
}
}
98 changes: 98 additions & 0 deletions http/policies.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
GET {{ host }}/xray/api/v1/policies
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{ token }}

###

POST {{ host }}/xray/api/v1/policies
Authorization: Bearer {{ token }}
Content-Type: application/json

{
"name": "terraform-test-policy",
"type": "security",
"description": "policy created by xray acceptance tests",
"rules": [
{
"name": "test-security-rule",
"priority": 1,
"criteria": {
"min_severity": "High"
},
"actions": {
"fail_build": false,
"block_download": {
"unscanned": true,
"active": true
},
"custom_severity": ""
}
}
]
}



###
POST {{ host }}/xray/api/v1/policies
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{ token }}

{
"name": "test-{{ $uuid }}",
"type": "security",
"description": "Watch on artifactory images uploaded",
"author": "{{ $env.USER }}",
"rules": [
{
"name": "thing",
"priority": 1,
"actions": {
"webhooks": [
"jfrog-Slack-integration-a453b",
"jfrog-Slack-integration-c11c1"
],
"block_download": {
"unscanned": false,
"active": false
}
},
"criteria": {
"cvss_range": {
"from": 4.1,
"to": 9.4
}
}
}
]
}

###

PUT {{ host }}/xray/api/v1/policies/terraform-test-policy

{
"name": "terraform-test-policy",
"type": "security",
"description": "policy created by xray acceptance tests",
"rules": [
{
"name": "test-security-rule",
"priority": 1,
"criteria": {
"min_severity": "High"
},
"actions": {
"fail_build": false,
"block_download": {
"unscanned": true,
"active": true
},
"custom_severity": ""
}
}
]
}

27 changes: 13 additions & 14 deletions pkg/artifactory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,27 @@ func Provider() *schema.Provider {
"url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_URL", func() (interface{}, error) {
return "http://localhost:8082", nil
}),
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_URL", "http://localhost:8082"),
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_USERNAME", func() (interface{}, error) {
return "admin", nil
}),
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_USERNAME", nil),
ConflictsWith: []string{"access_token", "api_key"},
ValidateFunc: validation.StringIsNotEmpty,
Deprecated: "Xray and projects functionality will not work with any auth method other than access tokens (Bearer)",

},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_PASSWORD", func() (interface{}, error) {
return "password", nil
}),
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_PASSWORD", nil),
ConflictsWith: []string{"access_token", "api_key"},
ValidateFunc: validation.StringIsNotEmpty,
Deprecated: "Xray and projects functionality will not work with any auth method other than access tokens (Bearer)",
Description: "Insider note: You may actually use an api_key as the password. This will get your around xray limitations instead of a bearer token",
},
"api_key": {
Type: schema.TypeString,
Expand All @@ -53,13 +51,15 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_API_KEY", nil),
ConflictsWith: []string{"username", "access_token", "password"},
ValidateFunc: validation.StringIsNotEmpty,
Deprecated: "Xray and projects functionality will not work with any auth method other than access tokens (Bearer)",
},
"access_token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_ACCESS_TOKEN", nil),
ConflictsWith: []string{"api_key", "password"},
Description: "This is a bearer token that can be given to you by your admin under `Identity and Access`",
},
},

Expand Down Expand Up @@ -128,15 +128,14 @@ func buildResty(URL string) (*resty.Client, error) {
}

func addAuthToResty(client *resty.Client, username, password, apiKey, accessToken string) (*resty.Client, error) {

if username != "" && password != "" {
return client.SetBasicAuth(username, password), nil
if accessToken != "" {
return client.SetAuthToken(accessToken), nil
}
if apiKey != "" {
return client.SetHeader("X-JFrog-Art-Api", apiKey), nil
}
if accessToken != "" {
return client.SetAuthToken(accessToken), nil
if username != "" && password != "" {
return client.SetBasicAuth(username, password), nil
}
return nil, fmt.Errorf("no authentication details supplied")
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/artifactory/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("ARTIFACTORY_URL"); v == "" {
t.Fatal("ARTIFACTORY_URL must be set for acceptance tests")
}

resty, err := buildResty(os.Getenv("ARTIFACTORY_URL"))
if err != nil {
t.Fatal(err)
}
username := os.Getenv("ARTIFACTORY_USERNAME")
password := os.Getenv("ARTIFACTORY_PASSWORD")
api := os.Getenv("ARTIFACTORY_APIKEY")
accessToken := os.Getenv("ARTIFACTORY_ACCESS_TOKEN")

if (username == "" || password == "") && api == "" && accessToken == "" {
t.Fatal("either ARTIFACTORY_USERNAME/ARTIFACTORY_PASSWORD or ARTIFACTORY_APIKEY or ARTIFACTORY_ACCESS_TOKEN must be set for acceptance test")
resty, err = addAuthToResty(resty, username, password, api, accessToken)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
err := testAccProvider.Configure(ctx, terraform.NewResourceConfigRaw(nil))
// TODO check the payload and make sure it's the right license type
_, err = resty.R().Get("/artifactory/api/system/licenses/")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
oldErr := testAccProvider.Configure(ctx, terraform.NewResourceConfigRaw(nil))
if oldErr != nil {
t.Fatal(oldErr)
}
}
17 changes: 10 additions & 7 deletions pkg/artifactory/resource_xray_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package artifactory

import (
"fmt"
"github.com/go-resty/resty/v2"
"log"
"net/http"

"github.com/go-resty/resty/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -25,7 +24,7 @@ type PolicyRuleCriteria struct {
CVSSRange *PolicyCVSSRange `json:"cvss_range,omitempty"`

// License Criteria
AllowUnknown *bool `json:"allow_unknown,omitempty"`
AllowUnknown *bool `json:"allow_unknown,omitempty"`
BannedLicenses *[]string `json:"banned_licenses,omitempty"`
AllowedLicenses *[]string `json:"allowed_licenses,omitempty"`
}
Expand Down Expand Up @@ -62,10 +61,14 @@ type Policy struct {

func resourceXrayPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceXrayPolicyCreate,
Read: resourceXrayPolicyRead,
Update: resourceXrayPolicyUpdate,
Delete: resourceXrayPolicyDelete,
SchemaVersion: 1,
Create: resourceXrayPolicyCreate,
Read: resourceXrayPolicyRead,
Update: resourceXrayPolicyUpdate,
Delete: resourceXrayPolicyDelete,
DeprecationMessage: "This portion of the provider uses V1 apis and will eventually be removed",
Description: "Creates an xray policy using V1 of the underlying APIs. Please note: " +
"It's only compatible with Bearer token auth method (Identity and Access => Access Tokens",

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down
Loading

0 comments on commit 43214b8

Please sign in to comment.