Skip to content

Commit 8513d91

Browse files
Deprecated registryProviderFlag and updated docs (#382)
* Deprecated registryProviderFlag and updated docs * fix linting and test errors --------- Co-authored-by: Sami Alajrami <[email protected]>
1 parent de247ce commit 8513d91

File tree

6 files changed

+42
-219
lines changed

6 files changed

+42
-219
lines changed

cmd/kosli/cli_utils.go

Lines changed: 5 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"net/http"
87
urlPackage "net/url"
98
"os"
109
"path/filepath"
@@ -19,7 +18,6 @@ import (
1918
"github.com/kosli-dev/cli/internal/digest"
2019
"github.com/kosli-dev/cli/internal/gitview"
2120
log "github.com/kosli-dev/cli/internal/logger"
22-
"github.com/kosli-dev/cli/internal/requests"
2321
"github.com/kosli-dev/cli/internal/utils"
2422
cp "github.com/otiai10/copy"
2523
"github.com/spf13/cobra"
@@ -311,92 +309,6 @@ func GetFlagFromVarName(varName string) string {
311309
return result
312310
}
313311

314-
type registryProviderEndpoints struct {
315-
mainApi string
316-
authApi string
317-
service string
318-
}
319-
320-
func getRegistryEndpointForProvider(provider string) (*registryProviderEndpoints, error) {
321-
switch provider {
322-
case "dockerhub":
323-
return &registryProviderEndpoints{
324-
mainApi: "https://registry-1.docker.io/v2",
325-
authApi: "https://auth.docker.io",
326-
service: "registry.docker.io",
327-
}, nil
328-
case "github":
329-
return &registryProviderEndpoints{
330-
mainApi: "https://ghcr.io/v2",
331-
authApi: "https://ghcr.io",
332-
service: "ghcr.io",
333-
}, nil
334-
335-
default:
336-
return getRegistryEndpoint(provider)
337-
}
338-
}
339-
340-
func getRegistryEndpoint(url string) (*registryProviderEndpoints, error) {
341-
url = strings.TrimPrefix(url, "https://")
342-
url = strings.Split(url, "/")[0]
343-
344-
return &registryProviderEndpoints{
345-
mainApi: "https://" + url + "/v2",
346-
authApi: "https://" + url + "/oauth2",
347-
service: url,
348-
}, nil
349-
}
350-
351-
// getDockerRegistryAPIToken returns a short-lived read-only api token for a docker registry api
352-
func getDockerRegistryAPIToken(providerInfo *registryProviderEndpoints, username, password, imageName string) (string, error) {
353-
var res *requests.HTTPResponse
354-
var err error
355-
356-
if strings.Contains(providerInfo.service, "jfrog") {
357-
url := "https://" + providerInfo.service + "/artifactory/api/security/token"
358-
359-
form := urlPackage.Values{}
360-
form.Add("username", username)
361-
form.Add("scope", "member-of-groups:readers")
362-
form.Add("expires_in", "60")
363-
364-
reqParams := &requests.RequestParams{
365-
Method: http.MethodPost,
366-
URL: url,
367-
Payload: form.Encode(),
368-
Username: username,
369-
Password: password,
370-
AdditionalHeaders: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
371-
}
372-
res, err = kosliClient.Do(reqParams)
373-
} else {
374-
url := fmt.Sprintf("%s/token?scope=repository:%s:pull&service=%s", providerInfo.authApi, imageName, providerInfo.service)
375-
reqParams := &requests.RequestParams{
376-
Method: http.MethodGet,
377-
URL: url,
378-
Username: username,
379-
Password: password,
380-
}
381-
res, err = kosliClient.Do(reqParams)
382-
}
383-
384-
if err != nil {
385-
return "", fmt.Errorf("failed to create an authentication token for the docker registry: %v %v", err, res)
386-
}
387-
388-
var responseData map[string]interface{}
389-
err = json.Unmarshal([]byte(res.Body), &responseData)
390-
if err != nil {
391-
return "", err
392-
}
393-
token := responseData["token"]
394-
if token == nil {
395-
token = responseData["access_token"]
396-
}
397-
return token.(string), nil
398-
}
399-
400312
// GetSha256Digest calculates the sha256 digest of an artifact.
401313
// Supported artifact types are: dir, file, docker
402314
func GetSha256Digest(artifactName string, o *fingerprintOptions, logger *log.Logger) (string, error) {
@@ -410,42 +322,8 @@ func GetSha256Digest(artifactName string, o *fingerprintOptions, logger *log.Log
410322
case "oci":
411323
fingerprint, err = digest.OciSha256(artifactName, o.registryUsername, o.registryPassword)
412324
case "docker":
413-
if o.registryProvider != "" {
414-
var providerInfo *registryProviderEndpoints
415-
providerInfo, err = getRegistryEndpointForProvider(o.registryProvider)
416-
if err != nil {
417-
return "", err
418-
}
419-
420-
nameSlice := strings.Split(artifactName, ":")
421-
if len(nameSlice) < 2 {
422-
nameSlice = append(nameSlice, "latest")
423-
}
424-
imageName := nameSlice[0]
425-
imageTag := nameSlice[1]
426-
427-
if strings.Contains(nameSlice[0], "/") {
428-
strSlice := strings.Split(nameSlice[0], "/")
429-
urlOrRepo := strSlice[0]
430-
if strings.Contains(urlOrRepo, ".") {
431-
imageName = strings.TrimPrefix(nameSlice[0], urlOrRepo+"/")
432-
}
433-
}
434-
435-
if !strings.Contains(imageName, "/") && o.registryProvider == "dockerhub" {
436-
imageName = fmt.Sprintf("library/%s", imageName)
437-
}
438-
439-
token, err := getDockerRegistryAPIToken(providerInfo, o.registryUsername, o.registryPassword, imageName)
440-
if err != nil {
441-
return "", err
442-
}
443-
444-
fingerprint, err = digest.RemoteDockerImageSha256(imageName, imageTag, providerInfo.mainApi, token, logger)
445-
if err != nil {
446-
return "", err
447-
}
448-
325+
if o.registryUsername != "" {
326+
fingerprint, err = digest.OciSha256(artifactName, o.registryUsername, o.registryPassword)
449327
} else {
450328
fingerprint, err = digest.DockerImageSha256(artifactName)
451329
}
@@ -540,13 +418,10 @@ func ValidateAttestationArtifactArg(args []string, artifactType, inputSha256 str
540418
// remote digest.
541419
func ValidateRegistryFlags(cmd *cobra.Command, o *fingerprintOptions) error {
542420
if o.artifactType != "docker" && o.artifactType != "oci" && (o.registryPassword != "" || o.registryUsername != "") {
543-
return ErrorBeforePrintingUsage(cmd, "--registry-provider, --registry-username and registry-password are only applicable when --artifact-type is 'docker'")
544-
}
545-
if o.registryProvider != "" && (o.registryPassword == "" || o.registryUsername == "") {
546-
return ErrorBeforePrintingUsage(cmd, "both --registry-username and registry-password are required when --registry-provider is used")
421+
return ErrorBeforePrintingUsage(cmd, "--registry-username and registry-password are only applicable when --artifact-type is 'docker' or 'oci'")
547422
}
548-
if o.registryProvider == "" && o.artifactType != "oci" && (o.registryPassword != "" || o.registryUsername != "") {
549-
return ErrorBeforePrintingUsage(cmd, "--registry-username and registry-password are only used when --registry-provider is used")
423+
if (o.registryPassword == "" && o.registryUsername != "") || (o.registryPassword != "" && o.registryUsername == "") {
424+
return ErrorBeforePrintingUsage(cmd, "--registry-username and registry-password must both be set")
550425
}
551426
return nil
552427
}

cmd/kosli/cli_utils_test.go

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -561,45 +561,6 @@ func (suite *CliUtilsTestSuite) TestValidateArtifactArg() {
561561
}
562562
}
563563

564-
func (suite *CliUtilsTestSuite) TestGetRegistryEndpointForProvider() {
565-
for _, t := range []struct {
566-
name string
567-
provider string
568-
want *registryProviderEndpoints
569-
expectError bool
570-
}{
571-
{
572-
name: "github provider returns expected endpoints",
573-
provider: "github",
574-
want: &registryProviderEndpoints{
575-
mainApi: "https://ghcr.io/v2",
576-
authApi: "https://ghcr.io",
577-
service: "ghcr.io",
578-
},
579-
},
580-
{
581-
name: "dockerhub provider returns expected endpoints",
582-
provider: "dockerhub",
583-
want: &registryProviderEndpoints{
584-
mainApi: "https://registry-1.docker.io/v2",
585-
authApi: "https://auth.docker.io",
586-
service: "registry.docker.io",
587-
},
588-
},
589-
} {
590-
suite.Run(t.name, func() {
591-
endpoints, err := getRegistryEndpointForProvider(t.provider)
592-
if t.expectError {
593-
require.Errorf(suite.T(), err, "error was expected but got none")
594-
} else {
595-
require.NoErrorf(suite.T(), err, "error was NOT expected but got %v", err)
596-
require.Equalf(suite.T(), t.want, endpoints,
597-
"TestGetRegistryEndpointForProvider: got %v -- want %v", t.want, endpoints)
598-
}
599-
})
600-
}
601-
}
602-
603564
func (suite *CliUtilsTestSuite) TestValidateRegistryFlags() {
604565
for _, t := range []struct {
605566
name string
@@ -610,16 +571,14 @@ func (suite *CliUtilsTestSuite) TestValidateRegistryFlags() {
610571
name: "registry flags are valid",
611572
options: &fingerprintOptions{
612573
artifactType: "docker",
613-
registryProvider: "dockerhub",
614574
registryUsername: "user",
615575
registryPassword: "pass",
616576
},
617577
},
618578
{
619-
name: "non-docker type with registry flags set casues an error",
579+
name: "non-docker type with registry flags set causes an error",
620580
options: &fingerprintOptions{
621581
artifactType: "file",
622-
registryProvider: "dockerhub",
623582
registryUsername: "user",
624583
registryPassword: "pass",
625584
},
@@ -629,7 +588,6 @@ func (suite *CliUtilsTestSuite) TestValidateRegistryFlags() {
629588
name: "missing username causes an error",
630589
options: &fingerprintOptions{
631590
artifactType: "docker",
632-
registryProvider: "dockerhub",
633591
registryPassword: "pass",
634592
},
635593
expectError: true,
@@ -638,36 +596,10 @@ func (suite *CliUtilsTestSuite) TestValidateRegistryFlags() {
638596
name: "missing password causes an error",
639597
options: &fingerprintOptions{
640598
artifactType: "docker",
641-
registryProvider: "dockerhub",
642599
registryUsername: "user",
643600
},
644601
expectError: true,
645602
},
646-
{
647-
name: "missing provider causes an error 1",
648-
options: &fingerprintOptions{
649-
artifactType: "docker",
650-
registryUsername: "user",
651-
registryPassword: "pass",
652-
},
653-
expectError: true,
654-
},
655-
{
656-
name: "missing provider causes an error 2",
657-
options: &fingerprintOptions{
658-
artifactType: "docker",
659-
registryUsername: "user",
660-
},
661-
expectError: true,
662-
},
663-
{
664-
name: "missing provider causes an error 3",
665-
options: &fingerprintOptions{
666-
artifactType: "docker",
667-
registryPassword: "pass",
668-
},
669-
expectError: true,
670-
},
671603
} {
672604
suite.Run(t.name, func() {
673605
err := ValidateRegistryFlags(&cobra.Command{}, t.options)

cmd/kosli/fingerprint.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ plus the ability to use recursive globs "**"
1919

2020
const fingerprintLongDesc = fingerprintShortDesc + `
2121
Requires ^--artifact-type^ flag to be set.
22-
Artifact type can be one of: "file" for files, "dir" for directories, "docker" for docker images.
22+
Artifact type can be one of: "file" for files, "dir" for directories, "oci" for container
23+
images in registries or "docker" for local docker images.
2324
24-
Fingerprinting docker images can be done using the local docker daemon or the fingerprint can be fetched
25+
Fingerprinting container images can be done using the local docker daemon or the fingerprint can be fetched
2526
from a remote registry.
2627
2728
` + fingerprintDirSynopsis
@@ -36,8 +37,14 @@ kosli fingerprint --artifact-type dir mydir
3637
# fingerprint a dir while excluding paths
3738
kosli fingerprint --artifact-type dir --exclude logs --exclude *.exe mydir
3839
39-
# fingerprint a locally available docker image
40+
# fingerprint a locally available docker image (requires docker daemon running)
4041
kosli fingerprint --artifact-type docker nginx:latest
42+
43+
# fingerprint a public image from a remote registry
44+
kosli fingerprint --artifact-type oci nginx:latest
45+
46+
# fingerprint a private image from a remote registry
47+
kosli fingerprint --artifact-type oci private:latest --registry-username YourUsername --registry-password YourPassword
4148
`
4249

4350
type fingerprintOptions struct {
@@ -74,6 +81,7 @@ func newFingerprintCmd(out io.Writer) *cobra.Command {
7481
err = DeprecateFlags(cmd, map[string]string{
7582
"e": "use -x instead",
7683
})
84+
7785
if err != nil {
7886
logger.Error("failed to configure deprecated flags: %v", err)
7987
}

cmd/kosli/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
ghUtils "github.com/kosli-dev/cli/internal/github"
88
gitlabUtils "github.com/kosli-dev/cli/internal/gitlab"
99
"github.com/spf13/cobra"
10+
"log"
1011
)
1112

1213
// allowed commit redaction values
@@ -22,6 +23,14 @@ func addFingerprintFlags(cmd *cobra.Command, o *fingerprintOptions) {
2223
cmd.Flags().StringVar(&o.registryUsername, "registry-username", "", registryUsernameFlag)
2324
cmd.Flags().StringVar(&o.registryPassword, "registry-password", "", registryPasswordFlag)
2425
cmd.Flags().StringSliceVarP(&o.excludePaths, "exclude", "x", []string{}, excludePathsFlag)
26+
27+
err := DeprecateFlags(cmd, map[string]string{
28+
"registry-provider": "no longer used",
29+
})
30+
31+
if err != nil {
32+
log.Fatalf("failed to configure deprecated flags: %v", err)
33+
}
2534
}
2635

2736
func addAWSAuthFlags(cmd *cobra.Command, o *aws.AWSStaticCreds) {

0 commit comments

Comments
 (0)