Skip to content

Commit 2b6a04a

Browse files
authored
Merge pull request #240 from nicholasSUSE/omit-endpoint
Security Update
2 parents f9f3254 + 04430ea commit 2b6a04a

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

main.go

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ const (
6666
defaultOverrideVersionEnvironmentVariable = "OVERRIDE_VERSION"
6767
// defaultMultiRCEnvironmentVariable is the default environment variable that indicates if the auto-bump should not remove previous RC versions
6868
defaultMultiRCEnvironmentVariable = "MULTI_RC"
69+
// Docker Registry authentication
70+
defaultDockerUserEnvironmentVariable = "DOCKER_USER"
71+
defaultDockerPasswordEnvironmentVariable = "DOCKER_PASSWORD"
6972
// Prime Registry authentication
7073
defaultPrimeUserEnvironmentVariable = "PRIME_USER"
7174
defaultPrimePasswordEnvironmentVariable = "PRIME_PASSWORD"
75+
defaultPrimeURLEnvironmentVariable = "PRIME_URL"
7276
)
7377

7478
var (
@@ -112,10 +116,16 @@ var (
112116
OverrideVersion string
113117
// MultiRC indicates if the auto-bump should not remove previous RC versions
114118
MultiRC bool
119+
// DockerUser is the username provided by EIO
120+
DockerUser string
121+
// DockerPassword is the password provided by EIO
122+
DockerPassword string
115123
// PrimeUser is the username provided by EIO
116124
PrimeUser string
117125
// PrimePassword is the password provided by EIO
118126
PrimePassword string
127+
// PrimeURL of SUSE Prime registry
128+
PrimeURL string
119129
)
120130

121131
func init() {
@@ -281,6 +291,20 @@ func main() {
281291
EnvVar: defaultGHTokenEnvironmentVariable,
282292
Destination: &GithubToken,
283293
}
294+
dockerUserFlag := cli.StringFlag{
295+
Name: "docker-user",
296+
Usage: "--docker-user=******** || DOCKER_USER=*******",
297+
Required: true,
298+
EnvVar: defaultDockerUserEnvironmentVariable,
299+
Destination: &DockerUser,
300+
}
301+
dockerPasswordFlag := cli.StringFlag{
302+
Name: "docker-password",
303+
Usage: "--docker-password=******** || DOCKER_PASSWORD=*******",
304+
Required: true,
305+
EnvVar: defaultDockerPasswordEnvironmentVariable,
306+
Destination: &DockerPassword,
307+
}
284308
primeUserFlag := cli.StringFlag{
285309
Name: "prime-user",
286310
Usage: "--prime-user=******** || PRIME_USER=*******",
@@ -295,6 +319,13 @@ func main() {
295319
EnvVar: defaultPrimePasswordEnvironmentVariable,
296320
Destination: &PrimePassword,
297321
}
322+
primeURLFlag := cli.StringFlag{
323+
Name: "prime-url",
324+
Usage: "--prime-url=******** || PRIME_URL=*******",
325+
Required: true,
326+
EnvVar: defaultPrimeURLEnvironmentVariable,
327+
Destination: &PrimeURL,
328+
}
298329
prNumberFlag := cli.StringFlag{
299330
Name: "pr_number",
300331
Usage: `Usage:
@@ -365,13 +396,13 @@ func main() {
365396
Name: "scan-registries",
366397
Usage: "Fetch, list and compare SUSE's registries and create yaml files with what is supposed to be synced from Docker Hub",
367398
Action: scanRegistries,
368-
Flags: []cli.Flag{},
399+
Flags: []cli.Flag{primeURLFlag},
369400
},
370401
{
371402
Name: "sync-registries",
372403
Usage: "Fetch, list and compare SUSE's registries and create yaml files with what is supposed to be synced from Docker Hub",
373404
Action: syncRegistries,
374-
Flags: []cli.Flag{primeUserFlag, primePasswordFlag},
405+
Flags: []cli.Flag{dockerUserFlag, dockerPasswordFlag, primeUserFlag, primePasswordFlag, primeURLFlag},
375406
},
376407
{
377408
Name: "index",
@@ -593,7 +624,7 @@ func downloadIcon(c *cli.Context) {
593624

594625
func scanRegistries(c *cli.Context) {
595626
ctx := context.Background()
596-
if err := registries.Scan(ctx); err != nil {
627+
if err := registries.Scan(ctx, PrimeURL); err != nil {
597628
logger.Fatal(ctx, err.Error())
598629
}
599630
}
@@ -603,13 +634,19 @@ func syncRegistries(c *cli.Context) {
603634

604635
emptyUser := PrimeUser == ""
605636
emptyPass := PrimePassword == ""
606-
if emptyUser || emptyPass {
637+
emptyURL := PrimeURL == ""
638+
emptyDockerUser := DockerUser == ""
639+
emptyDockerPass := DockerPassword == ""
640+
if emptyUser || emptyPass || emptyURL || emptyDockerUser || emptyDockerPass {
607641
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("User Empty", emptyUser))
608642
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Password Empty", emptyPass))
609-
logger.Fatal(ctx, errors.New("no credentials provided for prime registry").Error())
643+
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("URL Empty", emptyURL))
644+
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Docker User Empty", emptyDockerUser))
645+
logger.Log(ctx, slog.LevelError, "missing credential", slog.Bool("Docker Pass Empty", emptyDockerPass))
646+
logger.Fatal(ctx, errors.New("no credentials provided for sync").Error())
610647
}
611648

612-
if err := registries.Sync(ctx, PrimeUser, PrimePassword); err != nil {
649+
if err := registries.Sync(ctx, PrimeUser, PrimePassword, PrimeURL, DockerUser, DockerPassword); err != nil {
613650
logger.Fatal(ctx, err.Error())
614651
}
615652
}

pkg/registries/cosign.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ type tagMap func(name.Reference, ...ociremote.Option) (name.Tag, error)
6868
//
6969
// There is only one destination:
7070
// - Prime Registry
71-
func Sync(ctx context.Context, username, password string) error {
72-
s, err := prepareSync(ctx, username, password)
71+
func Sync(ctx context.Context, primeUser, primePass, primeURL, dockerUser, dockerPass string) error {
72+
s, err := prepareSync(ctx, primeUser, primePass, dockerUser, dockerPass)
7373
if err != nil {
7474
return err
7575
}
@@ -99,7 +99,7 @@ func Sync(ctx context.Context, username, password string) error {
9999
for repo, tags := range stagingImageTags {
100100
for _, tag := range tags {
101101
s.repoImage = &repoImage{} // init/reset img/tag to be synced
102-
if err := s.copy(ctx, StagingURL, repo, tag); err != nil {
102+
if err := s.copy(ctx, StagingURL, repo, tag, primeURL); err != nil {
103103
return err
104104
}
105105
if err := s.push(ctx); err != nil {
@@ -115,7 +115,7 @@ func Sync(ctx context.Context, username, password string) error {
115115
for repo, tags := range dockerImageTags {
116116
for _, tag := range tags {
117117
s.repoImage = &repoImage{}
118-
if err := s.copy(ctx, DockerURL, repo, tag); err != nil {
118+
if err := s.copy(ctx, DockerURL, repo, tag, primeURL); err != nil {
119119
return err
120120
}
121121
if err := s.push(ctx); err != nil {
@@ -131,7 +131,7 @@ func Sync(ctx context.Context, username, password string) error {
131131

132132
// prepareSync checks if the prime credentials are provided and creates the synchronizer
133133
// with all the oci,naming and remote options needed.
134-
func prepareSync(ctx context.Context, username, password string) (*synchronizer, error) {
134+
func prepareSync(ctx context.Context, primeUser, primePass, dockerUser, dockerPass string) (*synchronizer, error) {
135135
// Use strict validation for pulling and pushing
136136
// These options control how image references (e.g., "myregistry/myimage:tag")
137137
// are parsed and validated by go-containerregistry's 'name' package.
@@ -146,15 +146,15 @@ func prepareSync(ctx context.Context, username, password string) (*synchronizer,
146146
// (needed for docker.io without login)
147147
tr := http.DefaultTransport.(*http.Transport).Clone()
148148
tr.TLSClientConfig = &tls.Config{
149-
InsecureSkipVerify: true,
149+
InsecureSkipVerify: false,
150150
}
151151

152152
// applied to the puller and subsequently used by cosign's oci/remote
153153
// package when fetching signed entities.
154154
clientOpts := []remote.Option{
155155
remote.WithContext(ctx),
156156
remote.WithUserAgent(uaString),
157-
remote.WithAuthFromKeychain(authn.DefaultKeychain),
157+
remote.WithAuth(&authn.Basic{Username: dockerUser, Password: dockerPass}),
158158
remote.WithTransport(tr),
159159
}
160160

@@ -178,7 +178,7 @@ func prepareSync(ctx context.Context, username, password string) (*synchronizer,
178178
// prime (destination) registry. They use explicit basic authentication?
179179
remoteOpts := []remote.Option{
180180
remote.WithContext(ctx),
181-
remote.WithAuth(&authn.Basic{Username: username, Password: password}),
181+
remote.WithAuth(&authn.Basic{Username: primeUser, Password: primePass}),
182182
}
183183

184184
// Create a new remote pusher with the prime registry's specific authentication.
@@ -215,15 +215,15 @@ func loadSyncYamlFile(ctx context.Context, path string) (map[string][]string, er
215215

216216
// copy calculates the proper reference for the given img/tag at source and destination.
217217
// pulls in memory the signatures (if any) and the entity itself.
218-
func (s *synchronizer) copy(ctx context.Context, registry, repo, tag string) error {
218+
func (s *synchronizer) copy(ctx context.Context, registry, repo, tag, primeURL string) error {
219219
logger.Log(ctx, slog.LevelInfo, "cosign check/copy to Prime",
220220
slog.String("registry", registry),
221221
slog.String("repository", repo),
222222
slog.String("tag", tag))
223223

224224
// Build targets
225225
srcTarget := registry + repo + ":" + tag
226-
dstTarget := PrimeURL + repo + ":" + tag
226+
dstTarget := primeURL + "/" + repo + ":" + tag
227227

228228
srcRef, err := name.ParseReference(srcTarget, s.nameOpts...)
229229
if err != nil {

pkg/registries/registries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ var chartsToIgnoreTags = map[string]string{
2626
// - stagingToPrime.yaml
2727
//
2828
// Which will be used by another process to sync images/tags to Prime registry.
29-
func Scan(ctx context.Context) error {
29+
func Scan(ctx context.Context, primeRegistry string) error {
3030
// check the state of current assets and prime/staging registries
31-
_, dockerToPrime, stagingToPrime, err := checkRegistriesImagesTags(ctx)
31+
_, dockerToPrime, stagingToPrime, err := checkRegistriesImagesTags(ctx, primeRegistry)
3232
if err != nil {
3333
return err
3434
}

pkg/registries/remote.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
)
2323

2424
const (
25-
// PrimeURL of SUSE Prime registry
26-
PrimeURL string = "registry.suse.com/"
2725
// StagingURL of SUSE Staging registry
2826
StagingURL string = "stgregistry.suse.com/"
2927
// DockerURL of images
@@ -45,7 +43,7 @@ var (
4543
// 3. Filter what is present only on DockerHub but not in the Prime Registry
4644
// 4. From the list only present on Docker, list what is present in the Staging Registry
4745
// 5. Split the difference (Docker only images/tags and Staging Also images/tags)
48-
func checkRegistriesImagesTags(ctx context.Context) (map[string][]string, map[string][]string, map[string][]string, error) {
46+
func checkRegistriesImagesTags(ctx context.Context, primeRegistry string) (map[string][]string, map[string][]string, map[string][]string, error) {
4947
logger.Log(ctx, slog.LevelInfo, "checking registries images and tags")
5048

5149
// List all repository tags on Docker Hub by walking the entire image dependencies across all charts
@@ -55,7 +53,7 @@ func checkRegistriesImagesTags(ctx context.Context) (map[string][]string, map[st
5553
}
5654

5755
// Prime registry
58-
primeImgTags, err := listRegistryImageTags(ctx, assetsImageTagMap, PrimeURL)
56+
primeImgTags, err := listRegistryImageTags(ctx, assetsImageTagMap, primeRegistry)
5957
if err != nil {
6058
logger.Log(ctx, slog.LevelError, "failed to check prime image tags", logger.Err(err))
6159
return nil, nil, nil, err
@@ -133,7 +131,8 @@ var fetchTagsFromRegistryRepo = func(ctx context.Context, registry, asset string
133131
options = append(options, remote.WithAuth(auth))
134132
}
135133
}
136-
if registry == PrimeURL {
134+
135+
if registry != DockerURL && registry != StagingURL && strings.Contains(registry, "registry") {
137136
if auth := primeCredentials(ctx); auth != nil {
138137
options = append(options, remote.WithAuth(auth))
139138
}

pkg/registries/remote_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ func Test_checkRegistriesImagesTags(t *testing.T) {
355355
output output
356356
}
357357

358+
const PrimeURL = "im-prime"
359+
358360
tests := []test{
359361
// success - staging -> prime sync needed
360362
{
@@ -628,7 +630,7 @@ func Test_checkRegistriesImagesTags(t *testing.T) {
628630
createAssetValuesRepoTagMap = tt.input.createAssetsMock
629631
listRegistryImageTags = tt.input.listRegistryMock
630632

631-
assetsImageTagMap, dockerToPrime, stagingToPrime, err := checkRegistriesImagesTags(ctx)
633+
assetsImageTagMap, dockerToPrime, stagingToPrime, err := checkRegistriesImagesTags(ctx, "im-prime")
632634
assertError(t, err, tt.output.err)
633635
require.Equal(t, tt.output.assetsImageTagMap, assetsImageTagMap)
634636
require.Equal(t, tt.output.dockerToPrime, dockerToPrime)

0 commit comments

Comments
 (0)