Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 committed Aug 14, 2024
2 parents c983a75 + d0578f3 commit c0c6853
Show file tree
Hide file tree
Showing 55 changed files with 465 additions and 140 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
- [Retrieve the Graph Scan Results](#retrieve-the-graph-scan-results)
- [Request Graph Enrich](#request-graph-enrich)
- [Retrieve the Graph Enrich Results](#retrieve-the-graph-enrich-results)
- [Get Token Validation Status](#get-token-validation-status)
- [Generate Vulnerabilities Report](#generate-vulnerabilities-report)
- [Get Vulnerabilities Report Details](#get-vulnerabilities-report-details)
- [Get Vulnerabilities Report Content](#get-vulnerabilities-report-content)
Expand Down Expand Up @@ -244,6 +245,13 @@
- [Creating New Evidence Service Manager](#creating-new-evidence-service-manager)
- [Using Evidence Services](#using-evidence-services)
- [Upload Evidence](#upload-evidence)
- [Metadata APIs](#metadata-apis)
- [Creating Metadata Service Manager](#creating-metadata-service-manager)
- [Creating Metadata Details](#creating-metadata-details)
- [Creating Metadata Service Config](#creating-metadata-service-config)
- [Creating New Metadata Service Manager](#creating-new-metadata-service-manager)
- [Using Metadata Services](#using-metadata-services)
- [Graphql query](#graphql-query)

## General

Expand Down Expand Up @@ -2153,6 +2161,10 @@ scanId, err := xrayManager.ImportGraph(graphImportParams)
enrichResults, err := xrayManager.GetImportGraphResults(scanId)
```
#### Get Token Validation Status
```go
isEnabled, err := xrayManager.IsTokenValidationEnabled()
```
#### Generate Vulnerabilities Report
Expand Down Expand Up @@ -2964,3 +2976,48 @@ evidenceDetails := evidenceService.EvidenceDetails{
}
body, err = evideceManager.UploadEvidence(evidenceDetails)
```
## Metadata APIs
### Creating Metadata Service Manager
#### Creating Metadata Details
```go
mdDetails := auth.NewMetadataDetails()
mdDetails.SetUrl("http://localhost:8081/metadata")
mdDetails.SetAccessToken("access-token")
// if client certificates are required
mdDetails.SetClientCertPath("path/to/.cer")
mdDetails.SetClientCertKeyPath("path/to/.key")
```
#### Creating Metadata Service Config
```go
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(mdDetails).
SetCertificatesPath(mdDetails.GetClientCertPath()).
// Optionally overwrite the default HTTP retries, which is set to 3.
SetHttpRetries(3).
Build()
```
#### Creating New Metadata Service Manager
```go
metadataManager, err := metadata.NewManager(serviceConfig)
```
### Using Metadata Services
#### Graphql query
```go
queryBytes := []byte(`{"query":"someGraphqlQuery"}`)

queryDetails := metadataService.QueryDetails{
Body: queryBytes,
}

body, err = metadataManager.GraphqlQuery(queryDetails)
```
3 changes: 1 addition & 2 deletions access/services/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package services
import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (ps *TokenService) RefreshAccessToken(params CreateTokenParams) (auth.Creat
// createAccessToken is used to create & refresh access tokens.
func (ps *TokenService) createAccessToken(params CreateTokenParams) (tokenInfo auth.CreateTokenResponseData, err error) {
httpDetails := ps.ServiceDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpDetails.Headers)
httpDetails.SetContentTypeApplicationJson()
if err = ps.handleUnauthenticated(params, &httpDetails); err != nil {
return tokenInfo, err
}
Expand Down
3 changes: 1 addition & 2 deletions access/services/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package services
import (
"encoding/json"
"fmt"
"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"
Expand Down Expand Up @@ -40,7 +39,7 @@ func (us *InviteService) InviteUser(email, source string) error {
if err != nil {
return errorutils.CheckError(err)
}
utils.SetContentType("application/json", &httpDetails.Headers)
httpDetails.SetContentTypeApplicationJson()
resp, body, err := us.client.SendPost(url, requestContent, &httpDetails)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions access/services/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path"
"time"

artifactoryutils "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"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (ls *LoginService) SendLoginAuthenticationRequest(uuid string) error {
return errorutils.CheckError(err)
}
httpClientsDetails := ls.ServiceDetails.CreateHttpClientDetails()
artifactoryutils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
resp, body, err := ls.client.SendPost(fullUrl, requestContent, &httpClientsDetails)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/discardBuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

buildinfo "github.com/jfrog/build-info-go/entities"

artifactoryutils "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"
Expand Down Expand Up @@ -65,7 +64,7 @@ func (ds *DiscardBuildsService) DiscardBuilds(params DiscardBuildsParams) error
}

httpClientsDetails := ds.getArtifactoryDetails().CreateHttpClientDetails()
artifactoryutils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()

resp, body, err := ds.client.SendPost(requestFullUrl, requestContent, &httpClientsDetails)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path"
"strings"

artifactoryutils "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"
Expand Down Expand Up @@ -71,7 +70,7 @@ func (ds *DistributeService) BuildDistribute(params BuildDistributionParams) err
}

httpClientsDetails := ds.getArtifactoryDetails().CreateHttpClientDetails()
artifactoryutils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()

resp, body, err := ds.client.SendPost(requestFullUrl, requestContent, &httpClientsDetails)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/dockerpromote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"path"

artifactoryutils "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"
Expand Down Expand Up @@ -58,7 +57,7 @@ func (ps *DockerPromoteService) PromoteDocker(params DockerPromoteParams) error

// Send POST request
httpClientsDetails := ps.GetArtifactoryDetails().CreateHttpClientDetails()
artifactoryutils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
resp, body, err := ps.client.SendPost(url, requestContent, &httpClientsDetails)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"

"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
Expand Down Expand Up @@ -42,7 +41,7 @@ func (drs *ExportService) Export(exportParams ExportParams) error {
}
log.Info(exportMessage)

utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
resp, body, err := drs.client.SendPost(drs.artDetails.GetUrl()+"api/export/system", requestContent, &httpClientsDetails)
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions artifactory/services/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"
"net/url"

"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"
Expand All @@ -26,7 +25,7 @@ func (fs *FederationService) SetArtifactoryDetails(rt auth.ServiceDetails) {

func (fs *FederationService) ConvertLocalToFederated(repoKey string) error {
httpClientsDetails := fs.ArtDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
var url = fs.ArtDetails.GetUrl() + "api/federation/migrate/" + url.PathEscape(repoKey)
log.Info("Converting local repository to federated repository...")
resp, body, err := fs.client.SendPost(url, nil, &httpClientsDetails)
Expand All @@ -43,7 +42,7 @@ func (fs *FederationService) ConvertLocalToFederated(repoKey string) error {

func (fs *FederationService) TriggerFederatedFullSyncAll(repoKey string) error {
httpClientsDetails := fs.ArtDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
var url = fs.ArtDetails.GetUrl() + "api/federation/fullSync/" + url.PathEscape(repoKey)
log.Info("Triggering full federated repository synchronisation...")
resp, body, err := fs.client.SendPost(url, nil, &httpClientsDetails)
Expand All @@ -60,7 +59,7 @@ func (fs *FederationService) TriggerFederatedFullSyncAll(repoKey string) error {

func (fs *FederationService) TriggerFederatedFullSyncMirror(repoKey string, mirrorUrl string) error {
httpClientsDetails := fs.ArtDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
var url = fs.ArtDetails.GetUrl() + "api/federation/fullSync/" + url.PathEscape(repoKey) + "?mirror=" + url.QueryEscape(mirrorUrl)
log.Info("Triggering federated repository synchronisation...")
resp, body, err := fs.client.SendPost(url, nil, &httpClientsDetails)
Expand Down
4 changes: 1 addition & 3 deletions artifactory/services/permissiontarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"net/http"

"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"
Expand Down Expand Up @@ -78,7 +76,7 @@ func (pts *PermissionTargetService) performRequest(params PermissionTargetParams
return errorutils.CheckError(err)
}
httpClientsDetails := pts.ArtDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
var url = pts.ArtDetails.GetUrl() + "api/v2/security/permissions/" + params.Name
var operationString string
var resp *http.Response
Expand Down
2 changes: 0 additions & 2 deletions artifactory/services/props.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,13 @@ func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (

func (ps *PropsService) sendDeleteRequest(logMsgPrefix, relativePath, setPropertiesUrl string) (resp *http.Response, body []byte, err error) {
log.Info(logMsgPrefix+"Deleting properties on:", relativePath)
log.Debug(logMsgPrefix+"Sending delete properties request:", setPropertiesUrl)
httpClientsDetails := ps.GetArtifactoryDetails().CreateHttpClientDetails()
resp, body, err = ps.client.SendDelete(setPropertiesUrl, nil, &httpClientsDetails)
return
}

func (ps *PropsService) sendPutRequest(logMsgPrefix, relativePath, setPropertiesUrl string) (resp *http.Response, body []byte, err error) {
log.Info(logMsgPrefix+"Setting properties on:", relativePath)
log.Debug(logMsgPrefix+"Sending set properties request:", setPropertiesUrl)
httpClientsDetails := ps.GetArtifactoryDetails().CreateHttpClientDetails()
resp, body, err = ps.client.SendPut(setPropertiesUrl, nil, &httpClientsDetails)
return
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"net/url"

"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"
Expand All @@ -32,7 +31,7 @@ func (rs *RepositoryService) performRequest(params interface{}, repoKey string)
return err
}
httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
httpClientsDetails.SetContentTypeApplicationJson()
var url = rs.ArtDetails.GetUrl() + "api/repositories/" + url.PathEscape(repoKey)
var operationString string
var resp *http.Response
Expand Down
4 changes: 2 additions & 2 deletions artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,14 @@ func logUploadResponse(logMsgPrefix string, resp *http.Response, body []byte, ch

func addExplodeHeader(httpClientsDetails *httputils.HttpClientDetails, isExplode bool) {
if isExplode {
utils.AddHeader("X-Explode-Archive", "true", &httpClientsDetails.Headers)
httpClientsDetails.AddHeader("X-Explode-Archive", "true")
}
}

func (us *UploadService) doChecksumDeploy(details *fileutils.FileDetails, targetPath string, httpClientsDetails httputils.HttpClientDetails,
client *jfroghttpclient.JfrogHttpClient) (resp *http.Response, body []byte, err error) {
requestClientDetails := httpClientsDetails.Clone()
utils.AddHeader("X-Checksum-Deploy", "true", &requestClientDetails.Headers)
httpClientsDetails.AddHeader("X-Checksum-Deploy", "true")
utils.AddChecksumHeaders(requestClientDetails.Headers, details)
utils.AddAuthHeaders(requestClientDetails.Headers, us.ArtDetails)

Expand Down
14 changes: 8 additions & 6 deletions artifactory/services/utils/artifactoryutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func GetProjectQueryParam(projectKey string) string {
}

// paths - Sorted array.
// index - Index of the current path which we want to check if it a prefix of any of the other previous paths.
// index - Index of the current path which we want to check if it is a prefix of any of the other previous paths.
// separator - File separator.
// Returns true paths[index] is a prefix of any of the paths[i] where i<index, otherwise returns false.
func IsSubPath(paths []string, index int, separator string) bool {
Expand Down Expand Up @@ -205,7 +205,7 @@ func ParseNameAndVersion(identifier string, useLatestPolicy bool) (string, strin
identifiers := strings.Split(identifier, Delimiter)
// The delimiter must not be prefixed with escapeChar (if it is, it should be part of the version)
// the code below gets substring from before the last delimiter.
// If the new string ends with escape char it means the last delimiter was part of the version and we need
// If the new string ends with escape char it means the last delimiter was part of the version, and we need
// to go back to the previous delimiter.
// If no proper delimiter was found the full string will be the name.
for i := len(identifiers) - 1; i >= 1; i-- {
Expand Down Expand Up @@ -379,15 +379,15 @@ func loadMissingProperties(reader *content.ContentReader, readerWithProps *conte
return nil, err
}
reader.Reset()
if err := updateProps(readerWithProps, resultFile, buffer, writeOrder); err != nil {
if err = updateProps(readerWithProps, resultFile, buffer, writeOrder); err != nil {
return nil, err
}
return content.NewContentReader(resultFile.GetFilePath(), content.DefaultKey), nil
}

// Load the properties from readerWithProps into buffer's ResultItem and write its values into the resultWriter.
// buffer - Search result buffer Key -> relative path, value -> ResultItem. We use this to load the props into the item by matching the uniqueness of relevant path.
// crWithProps - File containing all the results with proprties.
// crWithProps - File containing all the results with properties.
// writeOrder - List of sorted buffer's searchResults(Map is an unordered collection).
// resultWriter - Search results (sorted) with props.
func updateProps(readerWithProps *content.ContentReader, resultWriter *content.ContentWriter, buffer map[string]*ResultItem, writeOrder []*ResultItem) error {
Expand All @@ -413,13 +413,15 @@ func updateProps(readerWithProps *content.ContentReader, resultWriter *content.C

// Run AQL to retrieve artifacts or dependencies which are associated with a specific build.
// Return a map of the items' SHA1.
func fetchBuildArtifactsOrDependenciesSha1(flags CommonConf, artifacts bool, builds []Build) (map[string]int, error) {
func fetchBuildArtifactsOrDependenciesSha1(flags CommonConf, artifacts bool, builds []Build) (buildArtifactsSha1 map[string]int, err error) {
buildQuery := createAqlQueryForBuild(buildIncludeQueryPart([]string{"name", "repo", "path", "actual_sha1"}), artifacts, builds)
reader, err := aqlSearch(buildQuery, flags)
if err != nil {
return nil, err
}
defer reader.Close()
defer func() {
err = errors.Join(err, reader.Close())
}()
return extractSha1FromAqlResponse(reader)
}

Expand Down
2 changes: 2 additions & 0 deletions artifactory/services/utils/tests/xray/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,8 @@ const xscVersionResponse = `{"xsc_version": "1.0.0"}`

const scanIdResponse = `{"scan_id": "3472b4e2-bddc-11ee-a9c9-acde48001122"}`

const JasConfigResponse = `{"enable_token_validation_scanning": true}`

const XscGitInfoResponse = `{"multi_scan_id": "3472b4e2-bddc-11ee-a9c9-acde48001122"}`

const XscGitInfoBadResponse = `"failed create git info request: git_repo_url field must contain value"`
Expand Down
12 changes: 12 additions & 0 deletions artifactory/services/utils/tests/xray/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ func enrichGetScanId(t *testing.T) func(w http.ResponseWriter, r *http.Request)
}
}

func getJasConfig(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, err := fmt.Fprint(w, JasConfigResponse)
assert.NoError(t, err)
return
}
http.Error(w, "Invalid enrich get scan id request", http.StatusBadRequest)
}
}

func enrichGetResults(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
Expand Down Expand Up @@ -250,6 +261,7 @@ func StartXrayMockServer(t *testing.T) int {
handlers["/xsc/api/v1/system/version"] = xscGetVersionHandlerFunc(t)
handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandlerFunc(t)
handlers["/xray/api/v1/scan/import_xml"] = enrichGetScanId(t)
handlers["/xray/api/v1/configuration/jas"] = getJasConfig(t)
getEnrichResults := fmt.Sprintf("/xray/api/v1/scan/graph/%s", TestMultiScanId)
handlers[getEnrichResults] = enrichGetResults(t)
handlers[fmt.Sprintf("/%s/", services.ReportsAPI)] = reportHandler
Expand Down
Loading

0 comments on commit c0c6853

Please sign in to comment.