Skip to content

Commit 6dcfc9c

Browse files
authored
feat(dl): add credential support for dl service (#163)
Signed-off-by: wuhuizuo <[email protected]> Signed-off-by: wuhuizuo <[email protected]>
1 parent 599d9c5 commit 6dcfc9c

File tree

5 files changed

+73
-39
lines changed

5 files changed

+73
-39
lines changed

dl/cmd/server/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func main() {
2828
secureF = flag.Bool("secure", false, "Use secure scheme (https or grpcs)")
2929
dbgF = flag.Bool("debug", false, "Log request and response bodies")
3030
ks3CfgPathF = flag.String("ks3-config", "ks3.yaml", "ks3 config yaml file path")
31+
ociCfgPathF = flag.String("oci-config", "oci.yaml", "oci config yaml file path")
3132
)
3233
flag.Parse()
3334

@@ -47,7 +48,7 @@ func main() {
4748
)
4849
{
4950
healthSvc = dl.NewHealth(logger)
50-
ociSvc = dl.NewOci(logger)
51+
ociSvc = dl.NewOci(logger, ociCfgPathF)
5152
ks3Svc = dl.NewKs3(logger, *ks3CfgPathF)
5253
}
5354

dl/ks3.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ type ks3srvc struct {
2525
}
2626

2727
func newKS3Client(cfg *pkgks3.Config) *s3.S3 {
28-
var cre = credentials.NewStaticCredentials(cfg.AccessKey, cfg.SecretKey, "")
28+
var cre *credentials.Credentials
29+
if cfg != nil && cfg.AccessKey != "" && cfg.SecretKey != "" {
30+
cre = credentials.NewStaticCredentials(cfg.AccessKey, cfg.SecretKey, "")
31+
}
2932
awsConfig := aws.Config{
3033
Region: cfg.Region, // Ref: https://docs.ksyun.com/documents/6761
3134
Credentials: cre,

dl/oci.go

+57-6
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,87 @@ import (
55
"io"
66
"log"
77
"net/url"
8+
"os"
89
"strings"
910

1011
oci "github.com/PingCAP-QE/ee-apps/dl/gen/oci"
1112
pkgoci "github.com/PingCAP-QE/ee-apps/dl/pkg/oci"
13+
"gopkg.in/yaml.v3"
14+
"oras.land/oras-go/v2/registry/remote"
15+
"oras.land/oras-go/v2/registry/remote/auth"
16+
"oras.land/oras-go/v2/registry/remote/retry"
1217
)
1318

1419
// oci service example implementation.
1520
// The example methods log the requests and return zero values.
1621
type ocisrvc struct {
17-
logger *log.Logger
22+
logger *log.Logger
23+
credential *auth.Credential
1824
}
1925

2026
// NewOci returns the oci service implementation.
21-
func NewOci(logger *log.Logger) oci.Service {
22-
return &ocisrvc{logger}
27+
func NewOci(logger *log.Logger, cfgFile *string) oci.Service {
28+
var cfg pkgoci.Config
29+
if cfgFile == nil {
30+
return &ocisrvc{logger: logger, credential: &auth.EmptyCredential}
31+
}
32+
33+
cfgBytes, err := os.ReadFile(*cfgFile)
34+
if err != nil {
35+
logger.Fatalf("Failed to load configuration: %v", err)
36+
}
37+
if err := yaml.Unmarshal(cfgBytes, &cfg); err != nil {
38+
logger.Fatalf("Failed to load configuration: %v", err)
39+
}
40+
41+
return &ocisrvc{logger: logger, credential: &auth.Credential{
42+
Username: cfg.Username,
43+
Password: cfg.Password,
44+
}}
2345
}
2446

2547
// ListFiles implements list-files.
2648
func (s *ocisrvc) ListFiles(ctx context.Context, p *oci.ListFilesPayload) (res []string, err error) {
2749
s.logger.Print("oci.list-files")
2850

29-
files, err := pkgoci.ListFiles(ctx, p.Repository, p.Tag)
51+
repository, err := s.getTargetRepo(p.Repository)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
files, err := pkgoci.ListFiles(ctx, repository, p.Tag)
3057
if err != nil {
3158
return nil, oci.MakeInvalidFilePath(err)
3259
}
3360

3461
return files, nil
3562
}
3663

64+
func (s *ocisrvc) getTargetRepo(repo string) (*remote.Repository, error) {
65+
repository, err := remote.NewRepository(repo)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
reg := strings.SplitN(repo, "/", 2)[0]
71+
repository.Client = &auth.Client{
72+
Client: retry.DefaultClient,
73+
Cache: auth.DefaultCache,
74+
Credential: auth.StaticCredential(reg, *s.credential),
75+
}
76+
77+
return repository, nil
78+
}
79+
3780
// DownloadFile implements download-file.
3881
func (s *ocisrvc) DownloadFile(ctx context.Context, p *oci.DownloadFilePayload) (res *oci.DownloadFileResult, resp io.ReadCloser, err error) {
3982
s.logger.Print("oci.download-files")
4083

41-
rc, length, err := pkgoci.NewFileReadCloser(ctx, p.Repository, p.Tag, p.File)
84+
repository, err := s.getTargetRepo(p.Repository)
85+
if err != nil {
86+
return nil, nil, err
87+
}
88+
rc, length, err := pkgoci.NewFileReadCloser(ctx, repository, p.Tag, p.File)
4289
if err != nil {
4390
return nil, nil, err
4491
}
@@ -54,7 +101,11 @@ func (s *ocisrvc) DownloadFile(ctx context.Context, p *oci.DownloadFilePayload)
54101
func (s *ocisrvc) DownloadFileSha256(ctx context.Context, p *oci.DownloadFileSha256Payload) (res *oci.DownloadFileSha256Result, resp io.ReadCloser, err error) {
55102
s.logger.Print("oci.download-file-sha256")
56103

57-
value, err := pkgoci.GetFileSHA256(ctx, p.Repository, p.Tag, p.File)
104+
repository, err := s.getTargetRepo(p.Repository)
105+
if err != nil {
106+
return nil, nil, err
107+
}
108+
value, err := pkgoci.GetFileSHA256(ctx, repository, p.Tag, p.File)
58109
if err != nil {
59110
return nil, nil, err
60111
}

dl/pkg/oci/cfg.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package oci
2+
3+
type Config struct {
4+
Username string `yaml:"username,omitempty" json:"username,omitempty"`
5+
Password string `yaml:"password,omitempty" json:"password,omitempty"`
6+
}

dl/pkg/oci/download.go

+4-31
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,14 @@ import (
66
"fmt"
77
"io"
88

9+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
910
oras "oras.land/oras-go/v2"
1011
"oras.land/oras-go/v2/registry/remote"
11-
12-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1312
)
1413

1514
const AnnotationKeyFileName = "org.opencontainers.image.title"
1615

17-
func ListFiles(ctx context.Context, repo, tag string) ([]string, error) {
18-
repository, err := remote.NewRepository(repo)
19-
if err != nil {
20-
return nil, err
21-
}
22-
23-
// Note: The below code can be omitted if authentication is not required
24-
// reg := strings.SplitN(repo, "/", 2)[0]
25-
// repository.Client = &auth.Client{
26-
// Client: retry.DefaultClient,
27-
// Cache: auth.DefaultCache,
28-
// Credential: auth.StaticCredential(reg, auth.Credential{
29-
// Username: "username",
30-
// Password: "password",
31-
// }),
32-
// }
33-
16+
func ListFiles(ctx context.Context, repository *remote.Repository, tag string) ([]string, error) {
3417
layers, err := listArtifactLayers(ctx, repository, tag)
3518
if err != nil {
3619
return nil, err
@@ -44,12 +27,7 @@ func ListFiles(ctx context.Context, repo, tag string) ([]string, error) {
4427
return ret, nil
4528
}
4629

47-
func NewFileReadCloser(ctx context.Context, repo, tag, filename string) (io.ReadCloser, int64, error) {
48-
repository, err := remote.NewRepository(repo)
49-
if err != nil {
50-
return nil, 0, err
51-
}
52-
30+
func NewFileReadCloser(ctx context.Context, repository *remote.Repository, tag, filename string) (io.ReadCloser, int64, error) {
5331
// 1. get desired file descriptor in the artifact.
5432
// destination := strings.Join([]string{repo, tag}, ":")
5533
desiredFileDescriptor, err := fetchFileDescriptor(ctx, repository, tag, filename)
@@ -67,12 +45,7 @@ func NewFileReadCloser(ctx context.Context, repo, tag, filename string) (io.Read
6745
return rc, desiredFileDescriptor.Size, nil
6846
}
6947

70-
func GetFileSHA256(ctx context.Context, repo, tag, filename string) (string, error) {
71-
repository, err := remote.NewRepository(repo)
72-
if err != nil {
73-
return "", err
74-
}
75-
48+
func GetFileSHA256(ctx context.Context, repository oras.ReadOnlyTarget, tag, filename string) (string, error) {
7649
// 1. get desired file descriptor in the artifact.
7750
// destination := strings.Join([]string{repo, tag}, ":")
7851
desiredFileDescriptor, err := fetchFileDescriptor(ctx, repository, tag, filename)

0 commit comments

Comments
 (0)