Skip to content

Commit f114953

Browse files
authored
Merge pull request #247 from nicholasSUSE/add-staging-creds
Add staging credentials
2 parents aee110d + 1a5a8be commit f114953

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ const (
6969
// Docker Registry authentication
7070
defaultDockerUserEnvironmentVariable = "DOCKER_USER"
7171
defaultDockerPasswordEnvironmentVariable = "DOCKER_PASSWORD"
72+
// Staging Registry authentication
73+
defaultStagingUserEnvironmentVariable = "STAGING_USER"
74+
defaultStagingPasswordEnvironmentVariable = "STAGING_PASSWORD"
7275
// Prime Registry authentication
7376
defaultPrimeUserEnvironmentVariable = "PRIME_USER"
7477
defaultPrimePasswordEnvironmentVariable = "PRIME_PASSWORD"
@@ -120,6 +123,10 @@ var (
120123
DockerUser string
121124
// DockerPassword is the password provided by EIO
122125
DockerPassword string
126+
// StagingUser is the username provided by EIO
127+
StagingUser string
128+
// StagingPassword is the password provided by EIO
129+
StagingPassword string
123130
// PrimeUser is the username provided by EIO
124131
PrimeUser string
125132
// PrimePassword is the password provided by EIO
@@ -305,6 +312,20 @@ func main() {
305312
EnvVar: defaultDockerPasswordEnvironmentVariable,
306313
Destination: &DockerPassword,
307314
}
315+
stagingUserFlag := cli.StringFlag{
316+
Name: "staging-user",
317+
Usage: "--staging-user=******** || STAGING_USER=*******",
318+
Required: true,
319+
EnvVar: defaultStagingUserEnvironmentVariable,
320+
Destination: &StagingUser,
321+
}
322+
stagingPasswordFlag := cli.StringFlag{
323+
Name: "staging-password",
324+
Usage: "--staging-password=******** || STAGING_PASSWORD=*******",
325+
Required: true,
326+
EnvVar: defaultStagingPasswordEnvironmentVariable,
327+
Destination: &StagingPassword,
328+
}
308329
primeUserFlag := cli.StringFlag{
309330
Name: "prime-user",
310331
Usage: "--prime-user=******** || PRIME_USER=*******",
@@ -402,7 +423,7 @@ func main() {
402423
Name: "sync-registries",
403424
Usage: "Fetch, list and compare SUSE's registries and create yaml files with what is supposed to be synced from Docker Hub",
404425
Action: syncRegistries,
405-
Flags: []cli.Flag{dockerUserFlag, dockerPasswordFlag, primeUserFlag, primePasswordFlag, primeURLFlag},
426+
Flags: []cli.Flag{dockerUserFlag, dockerPasswordFlag, stagingUserFlag, stagingPasswordFlag, primeUserFlag, primePasswordFlag, primeURLFlag},
406427
},
407428
{
408429
Name: "index",
@@ -643,16 +664,20 @@ func syncRegistries(c *cli.Context) {
643664
emptyURL := PrimeURL == ""
644665
emptyDockerUser := DockerUser == ""
645666
emptyDockerPass := DockerPassword == ""
646-
if emptyUser || emptyPass || emptyURL || emptyDockerUser || emptyDockerPass {
667+
emptyStagingUser := StagingUser == ""
668+
emptyStagingPass := StagingPassword == ""
669+
if emptyUser || emptyPass || emptyURL || emptyDockerUser || emptyDockerPass || emptyStagingUser || emptyStagingPass {
647670
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("User Empty", emptyUser))
648671
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Password Empty", emptyPass))
649672
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("URL Empty", emptyURL))
650673
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Docker User Empty", emptyDockerUser))
651674
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Docker Pass Empty", emptyDockerPass))
675+
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Staging User Empty", emptyStagingUser))
676+
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Staging Pass Empty", emptyStagingPass))
652677
logger.Fatal(ctx, errors.New("no credentials provided for sync").Error())
653678
}
654679

655-
if err := registries.Sync(ctx, PrimeUser, PrimePassword, PrimeURL, DockerUser, DockerPassword); err != nil {
680+
if err := registries.Sync(ctx, PrimeUser, PrimePassword, PrimeURL, DockerUser, DockerPassword, StagingUser, StagingPassword); err != nil {
656681
logger.Fatal(ctx, err.Error())
657682
}
658683
}

pkg/registries/cosign.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ type repoImage struct {
4545

4646
// sourceRegistry will be either Staging Registry or Docker Hub
4747
type sourceRegistry struct {
48-
ociOpts []ociremote.Option
48+
dockerOpts []ociremote.Option
49+
stagingOpts []ociremote.Option
4950
}
5051

5152
type primeRegistry struct {
@@ -68,8 +69,8 @@ type tagMap func(name.Reference, ...ociremote.Option) (name.Tag, error)
6869
//
6970
// There is only one destination:
7071
// - Prime Registry
71-
func Sync(ctx context.Context, primeUser, primePass, primeURL, dockerUser, dockerPass string) error {
72-
s, err := prepareSync(ctx, primeUser, primePass, dockerUser, dockerPass)
72+
func Sync(ctx context.Context, primeUser, primePass, primeURL, dockerUser, dockerPass, stagingUser, stagingPass string) error {
73+
s, err := prepareSync(ctx, primeUser, primePass, dockerUser, dockerPass, stagingUser, stagingPass)
7374
if err != nil {
7475
return err
7576
}
@@ -131,7 +132,7 @@ func Sync(ctx context.Context, primeUser, primePass, primeURL, dockerUser, docke
131132

132133
// prepareSync checks if the prime credentials are provided and creates the synchronizer
133134
// with all the oci,naming and remote options needed.
134-
func prepareSync(ctx context.Context, primeUser, primePass, dockerUser, dockerPass string) (*synchronizer, error) {
135+
func prepareSync(ctx context.Context, primeUser, primePass, dockerUser, dockerPass, stagingUser, staginPass string) (*synchronizer, error) {
135136
// Use strict validation for pulling and pushing
136137
// These options control how image references (e.g., "myregistry/myimage:tag")
137138
// are parsed and validated by go-containerregistry's 'name' package.
@@ -151,29 +152,43 @@ func prepareSync(ctx context.Context, primeUser, primePass, dockerUser, dockerPa
151152

152153
// applied to the puller and subsequently used by cosign's oci/remote
153154
// package when fetching signed entities.
154-
clientOpts := []remote.Option{
155+
dockerClientOpts := []remote.Option{
155156
remote.WithContext(ctx),
156157
remote.WithUserAgent(uaString),
157158
remote.WithAuth(&authn.Basic{Username: dockerUser, Password: dockerPass}),
158159
remote.WithTransport(tr),
159160
}
161+
stagingClientOpts := []remote.Option{
162+
remote.WithContext(ctx),
163+
remote.WithUserAgent(uaString),
164+
remote.WithAuth(&authn.Basic{Username: stagingUser, Password: staginPass}),
165+
remote.WithTransport(tr),
166+
}
160167

161168
// reuse new remote puller for efficiency across operations.
162169
// puller interacts only with docker.io and staging registry.
163-
puller, err := remote.NewPuller(clientOpts...)
170+
dockerPuller, err := remote.NewPuller(dockerClientOpts...)
164171
if err == nil {
165-
clientOpts = append(clientOpts, remote.Reuse(puller))
172+
dockerClientOpts = append(dockerClientOpts, remote.Reuse(dockerPuller))
173+
}
174+
stagingPuller, err := remote.NewPuller(stagingClientOpts...)
175+
if err == nil {
176+
stagingClientOpts = append(stagingClientOpts, remote.Reuse(stagingPuller))
166177
}
167178

168179
// These options are specifically for cosign's 'pkg/oci/remote' functions
169180
// (e.g., ociremote.SignedEntity, ociremote.SignatureTag). They bridge the
170181
// 'go-containerregistry' remote options to cosign's operations.
171-
pullerOpts := []ociremote.Option{
182+
dockerPullerOpts := []ociremote.Option{
183+
ociremote.WithNameOptions(nameOpts...),
184+
}
185+
stagingPullerOpts := []ociremote.Option{
172186
ociremote.WithNameOptions(nameOpts...),
173187
}
174188
// Embed the 'go-containerregistry' remote options
175189
// (context, user agent, keychain auth, insecure transport) into cosign's client setup.
176-
pullerOpts = append(pullerOpts, ociremote.WithRemoteOptions(clientOpts...))
190+
dockerPullerOpts = append(dockerPullerOpts, ociremote.WithRemoteOptions(dockerClientOpts...))
191+
stagingPullerOpts = append(stagingPullerOpts, ociremote.WithRemoteOptions(stagingClientOpts...))
177192

178193
// prime (destination) registry. They use explicit basic authentication?
179194
remoteOpts := []remote.Option{
@@ -190,7 +205,8 @@ func prepareSync(ctx context.Context, primeUser, primePass, dockerUser, dockerPa
190205
return &synchronizer{
191206
nameOpts: nameOpts,
192207
sourceRegistry: &sourceRegistry{
193-
ociOpts: pullerOpts,
208+
dockerOpts: dockerPullerOpts,
209+
stagingOpts: stagingPullerOpts,
194210
},
195211
primeRegistry: &primeRegistry{
196212
pusher: pusher,
@@ -262,7 +278,7 @@ func (s *synchronizer) pullFromDocker(ctx context.Context, imgRepo string) error
262278
// Get the base repository reference for the source image.
263279
srcRepoRef := s.repoImage.srcRef.Context()
264280

265-
root, err := ociremote.SignedEntity(s.repoImage.srcRef, s.sourceRegistry.ociOpts...)
281+
root, err := ociremote.SignedEntity(s.repoImage.srcRef, s.sourceRegistry.dockerOpts...)
266282
if err != nil {
267283
logger.Log(ctx, slog.LevelError, "signedEntity failure", logger.Err(err))
268284
return err
@@ -318,7 +334,7 @@ func (s *synchronizer) pullFromStaging(ctx context.Context, imgRepo string) erro
318334
dstRepoRef := s.repoImage.dstRef.Context()
319335

320336
// An oci.SignedEntity represents the image manifest itself AND all its associated
321-
root, err := ociremote.SignedEntity(s.repoImage.srcRef, s.sourceRegistry.ociOpts...)
337+
root, err := ociremote.SignedEntity(s.repoImage.srcRef, s.sourceRegistry.stagingOpts...)
322338
if err != nil {
323339
logger.Log(ctx, slog.LevelError, "signedEntity failure", logger.Err(err))
324340
return err
@@ -337,7 +353,7 @@ func (s *synchronizer) pullFromStaging(ctx context.Context, imgRepo string) erro
337353
copyTag := func(tm tagMap) error {
338354
// Construct the *expected name* (e.g., "myimage:sha256-digest.sig")
339355
// for the artifact based on its digest. This call does NOT confirm existence yet.
340-
src, err := tm(srcDigest, s.sourceRegistry.ociOpts...)
356+
src, err := tm(srcDigest, s.sourceRegistry.stagingOpts...)
341357
if err != nil {
342358
return err
343359
}

0 commit comments

Comments
 (0)