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
402314func 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.
541419func 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}
0 commit comments