Skip to content

Commit ef22d54

Browse files
committed
chore: ping registry to validate login credentials
1 parent ce5d968 commit ef22d54

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

cmd/registry_login.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
46
"github.com/highcard-dev/daemon/internal/core/domain"
7+
"github.com/highcard-dev/daemon/internal/core/services/registry"
58
"github.com/spf13/cobra"
69
"github.com/spf13/viper"
710
)
@@ -21,6 +24,13 @@ Examples:
2124
druid registry login --host artifacts.druid.gg/project1 -u user1 -p pass1
2225
druid registry login --host artifacts.druid.gg/project2 -u user2 -p pass2`,
2326
RunE: func(cmd *cobra.Command, args []string) error {
27+
28+
if err := registry.ValidateCredentials(registryHost, registryUser, registryPassword); err != nil {
29+
return fmt.Errorf("login failed: %w", err)
30+
}
31+
32+
cmd.Println("Login succeeded")
33+
2434
var registries []domain.RegistryCredential
2535
viper.UnmarshalKey("registries", &registries)
2636

internal/core/services/registry/oci.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ func extractHost(repoUrl string) string {
8181
return repoUrl
8282
}
8383

84+
func ValidateCredentials(host, username, password string) error {
85+
registryHost := extractHost(host)
86+
87+
reg, err := remote.NewRegistry(registryHost)
88+
if err != nil {
89+
return fmt.Errorf("invalid registry host: %w", err)
90+
}
91+
92+
reg.Client = &auth.Client{
93+
Client: retry.DefaultClient,
94+
Cache: auth.DefaultCache,
95+
Credential: auth.StaticCredential(registryHost, auth.Credential{
96+
Username: username,
97+
Password: password,
98+
}),
99+
}
100+
101+
if err := reg.Ping(context.Background()); err != nil {
102+
return fmt.Errorf("authentication failed: %w", err)
103+
}
104+
105+
return nil
106+
}
107+
84108
func (c *OciClient) Pull(dir string, artifact string) error {
85109
return c.PullSelective(dir, artifact, true, nil)
86110
}

0 commit comments

Comments
 (0)