-
Notifications
You must be signed in to change notification settings - Fork 11
/
registry-access.go
49 lines (37 loc) · 1.29 KB
/
registry-access.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package tufnotary
import (
"context"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/pkg/content"
"oras.land/oras-go/pkg/oras"
)
func UploadTUFMetadata(registry string, repository string, name string, contents []byte, reference string) (ocispec.Descriptor, error) {
ref := registry + "/" + repository + ":" + name
fileName := repository + "/staged/" + name + ".json"
mediaType := "application/vnd.cncf.notary.tuf+json"
ctx := context.Background()
// TODO: add reference once it's supported in oras-go: https://github.com/oras-project/oras-go/pull/35
memoryStore := content.NewMemory()
desc, err := memoryStore.Add(fileName, mediaType, contents)
if err != nil {
return ocispec.Descriptor{}, err
}
manifest, manifestDesc, config, configDesc, err := content.GenerateManifestAndConfig(nil, nil, desc)
if err != nil {
return ocispec.Descriptor{}, err
}
memoryStore.Set(configDesc, config)
err = memoryStore.StoreManifest(ref, manifestDesc, manifest)
if err != nil {
return ocispec.Descriptor{}, err
}
reg, err := content.NewRegistry(content.RegistryOptions{PlainHTTP: true})
if err != nil {
return ocispec.Descriptor{}, err
}
desc, err = oras.Copy(ctx, memoryStore, ref, reg, "")
if err != nil {
return ocispec.Descriptor{}, err
}
return desc, nil
}