-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
145a579
commit bb8b70e
Showing
7 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
) | ||
|
||
func NewEvidenceDetails() auth.ServiceDetails { | ||
return &evidenceDetails{} | ||
} | ||
|
||
type evidenceDetails struct { | ||
auth.CommonConfigFields | ||
} | ||
|
||
func (rt *evidenceDetails) GetVersion() (string, error) { | ||
panic("Failed: Method is not implemented") | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package evidence | ||
|
||
import ( | ||
"encoding/json" | ||
artifactoryAuth "github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
evidence "github.com/jfrog/jfrog-client-go/evidence/services" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
var dsseRaw = []byte("someData") | ||
|
||
var evidenceData = evidence.EvidenceDetails{ | ||
SubjectUri: "someUri", | ||
DSSEFileRaw: &dsseRaw, | ||
} | ||
|
||
func TestEvidenceServicesManager_UploadEvidence(t *testing.T) { | ||
handlerFunc, requestNum := createDefaultHandlerFunc(t) | ||
|
||
mockServer, evdService := createMockServer(t, handlerFunc) | ||
defer mockServer.Close() | ||
|
||
_, err := evdService.UploadEvidence(evidenceData) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, *requestNum) | ||
} | ||
|
||
func createMockServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, *evidence.EvidenceService) { | ||
testServer := httptest.NewServer(testHandler) | ||
|
||
rtDetails := artifactoryAuth.NewArtifactoryDetails() | ||
rtDetails.SetUrl(testServer.URL + "/") | ||
|
||
client, err := jfroghttpclient.JfrogClientBuilder().Build() | ||
assert.NoError(t, err) | ||
return testServer, evidence.NewEvidenceService(rtDetails, client) | ||
} | ||
|
||
func createDefaultHandlerFunc(t *testing.T) (http.HandlerFunc, *int) { | ||
requestNum := 0 | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/api/v1/subject" { | ||
w.WriteHeader(http.StatusOK) | ||
requestNum++ | ||
writeMockStatusResponse(t, w, dsseRaw) | ||
} | ||
}, &requestNum | ||
} | ||
|
||
func writeMockStatusResponse(t *testing.T, w http.ResponseWriter, payload []byte) { | ||
content, err := json.Marshal(payload) | ||
assert.NoError(t, err) | ||
_, err = w.Write(content) | ||
assert.NoError(t, err) | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package evidence | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/config" | ||
"github.com/jfrog/jfrog-client-go/evidence/services" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
) | ||
|
||
type EvidenceServicesManager struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
config config.Config | ||
} | ||
|
||
func New(config config.Config) (*EvidenceServicesManager, error) { | ||
details := config.GetServiceDetails() | ||
var err error | ||
manager := &EvidenceServicesManager{config: config} | ||
manager.client, err = jfroghttpclient.JfrogClientBuilder(). | ||
SetCertificatesPath(config.GetCertificatesPath()). | ||
SetInsecureTls(config.IsInsecureTls()). | ||
SetClientCertPath(details.GetClientCertPath()). | ||
SetClientCertKeyPath(details.GetClientCertKeyPath()). | ||
AppendPreRequestInterceptor(details.RunPreRequestFunctions). | ||
SetContext(config.GetContext()). | ||
SetDialTimeout(config.GetDialTimeout()). | ||
SetOverallRequestTimeout(config.GetOverallRequestTimeout()). | ||
SetRetries(config.GetHttpRetries()). | ||
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()). | ||
Build() | ||
|
||
return manager, err | ||
} | ||
|
||
func (esm *EvidenceServicesManager) Client() *jfroghttpclient.JfrogHttpClient { | ||
return esm.client | ||
} | ||
|
||
func (esm *EvidenceServicesManager) UploadEvidence(evidenceDetails services.EvidenceDetails) ([]byte, error) { | ||
evidenceService := services.NewEvidenceService(esm.config.GetServiceDetails(), esm.client) | ||
return evidenceService.UploadEvidence(evidenceDetails) | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package services | ||
|
||
import "path" | ||
|
||
const ( | ||
evidenceCreateAPI = "api/v1/subject" | ||
) | ||
|
||
type createEvidenceOperation struct { | ||
evidenceBody EvidenceCreationBody | ||
} | ||
|
||
func (c *createEvidenceOperation) getOperationRestApi() string { | ||
return path.Join(evidenceCreateAPI, c.evidenceBody.SubjectUri) | ||
} | ||
|
||
func (c *createEvidenceOperation) getRequestBody() *[]byte { | ||
return c.evidenceBody.DSSEFileRaw | ||
} | ||
|
||
func (c *createEvidenceOperation) getOperationSuccessfulMsg() string { | ||
return "Evidence is successfully created" | ||
} | ||
|
||
func (rbs *EvidenceService) UploadEvidence(evidenceDetails EvidenceDetails) ([]byte, error) { | ||
operation := createEvidenceOperation{ | ||
evidenceBody: EvidenceCreationBody{ | ||
EvidenceDetails: evidenceDetails, | ||
}, | ||
} | ||
body, err := rbs.doOperation(&operation) | ||
return body, err | ||
} | ||
|
||
type SourceBuildDetails struct { | ||
BuildName string | ||
BuildNumber string | ||
ProjectKey string | ||
} | ||
|
||
type EvidenceCreationBody struct { | ||
EvidenceDetails | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package services | ||
|
||
import ( | ||
rtUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type EvidenceService struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
evidenceDetails *auth.ServiceDetails | ||
} | ||
|
||
func NewEvidenceService(evidenceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *EvidenceService { | ||
return &EvidenceService{evidenceDetails: &evidenceDetails, client: client} | ||
} | ||
|
||
func (es *EvidenceService) GetEvidenceDetails() auth.ServiceDetails { | ||
return *es.evidenceDetails | ||
} | ||
|
||
type EvidenceOperation interface { | ||
getOperationRestApi() string | ||
getRequestBody() *[]byte | ||
getOperationSuccessfulMsg() string | ||
} | ||
|
||
func (es *EvidenceService) doOperation(operation EvidenceOperation) ([]byte, error) { | ||
u := url.URL{Path: operation.getOperationRestApi()} | ||
requestFullUrl, err := url.Parse(es.GetEvidenceDetails().GetUrl() + u.String()) | ||
if err != nil { | ||
return []byte{}, errorutils.CheckError(err) | ||
} | ||
|
||
httpClientDetails := es.GetEvidenceDetails().CreateHttpClientDetails() | ||
rtUtils.SetContentType("application/json", &httpClientDetails.Headers) | ||
|
||
resp, body, err := es.client.SendPost(requestFullUrl.String(), *operation.getRequestBody(), &httpClientDetails) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
return body, errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated) | ||
} | ||
|
||
type EvidenceDetails struct { | ||
SubjectUri string `json:"subject_uri"` | ||
DSSEFileRaw *[]byte `json:"dsse_file_raw"` | ||
} |
This file contains 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