|
| 1 | +package oci_registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + spec "github.com/opencontainers/image-spec/specs-go/v1" |
| 9 | + log "github.com/sirupsen/logrus" |
| 10 | + "oras.land/oras-go/v2" |
| 11 | + "oras.land/oras-go/v2/content" |
| 12 | + "oras.land/oras-go/v2/content/file" |
| 13 | + "oras.land/oras-go/v2/registry" |
| 14 | + "oras.land/oras-go/v2/registry/remote" |
| 15 | + "os" |
| 16 | + "path/filepath" |
| 17 | +) |
| 18 | + |
| 19 | +func GetOCIArtifactURLAndPathByLanguage(language, version string) (string, string, error) { |
| 20 | + userHomeDir, err := os.UserHomeDir() |
| 21 | + if err != nil { |
| 22 | + return "", "", err |
| 23 | + } |
| 24 | + artifactPath := userHomeDir + "/.compage/templates/compage-template-" + language + "/" + version |
| 25 | + artifactURL := "ghcr.io/intelops/compage-template-" + language + ":" + version |
| 26 | + if language == "common" { |
| 27 | + artifactPath = userHomeDir + "/.compage/templates/common-templates/" + version |
| 28 | + return "ghcr.io/intelops/common-templates:" + version, artifactPath, nil |
| 29 | + } |
| 30 | + return artifactURL, artifactPath, nil |
| 31 | +} |
| 32 | + |
| 33 | +// Pull pulls an OCI image from a registry. |
| 34 | +func Pull(requestCtx context.Context, disableTLS bool) (template *string, err error) { |
| 35 | + ociName := requestCtx.Value("artifactURL").(string) |
| 36 | + ref, err := registry.ParseReference(ociName) |
| 37 | + if err != nil { |
| 38 | + log.Errorf("parse reference: %v", err) |
| 39 | + return nil, fmt.Errorf("parse reference: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + if ref.Reference == ociLatestTag || ref.Reference == "" { |
| 43 | + ref.Reference, err = getLatestTagSortedBySemver(ref.Registry+"/"+ref.Repository, disableTLS) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("get latest tag sorted by semver: %w", err) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Connect to a remote repository |
| 50 | + ctx := context.Background() |
| 51 | + |
| 52 | + repository, err := remote.NewRepository(ociName) |
| 53 | + if err != nil { |
| 54 | + log.Errorf("new repository: %v", err) |
| 55 | + return nil, fmt.Errorf("new repository: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + if disableTLS { |
| 59 | + log.Debugln("TLS connection is disabled") |
| 60 | + repository.PlainHTTP = true |
| 61 | + } |
| 62 | + |
| 63 | + // Get credentials from the docker credential store |
| 64 | + if err = getCredentialsFromDockerStore(repository); err != nil { |
| 65 | + log.Errorf("credstore from docker: %v", err) |
| 66 | + return nil, fmt.Errorf("credstore from docker: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Get remote manifest digest |
| 70 | + remoteManifestSpec, remoteManifestReader, err := oras.Fetch(ctx, repository, ref.String(), oras.DefaultFetchOptions) |
| 71 | + if err != nil { |
| 72 | + log.Errorf("fetch: %v", err) |
| 73 | + return nil, fmt.Errorf("fetch: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + remoteManifestData, err := content.ReadAll(remoteManifestReader, remoteManifestSpec) |
| 77 | + if err != nil { |
| 78 | + log.Errorf("fetch remote content: %v", err) |
| 79 | + return nil, fmt.Errorf("fetch remote content: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Create the template root directory |
| 83 | + templateRootDir := requestCtx.Value("artifactPath").(string) |
| 84 | + |
| 85 | + fs, err := file.New(templateRootDir) |
| 86 | + if err != nil { |
| 87 | + log.Errorf("create file store: %v", err) |
| 88 | + return nil, fmt.Errorf("create file store: %w", err) |
| 89 | + } |
| 90 | + defer func(fs *file.Store) { |
| 91 | + _ = fs.Close() |
| 92 | + }(fs) |
| 93 | + |
| 94 | + // Copy from the remote repository to the file store |
| 95 | + // Fetch the remote manifest |
| 96 | + remoteTemplate, err := getCompageTemplateFromManifestLayers(remoteManifestData, templateRootDir) |
| 97 | + |
| 98 | + template = remoteTemplate |
| 99 | + |
| 100 | + manifestDescriptor, err := oras.Copy(ctx, repository, ref.Reference, fs, ref.Reference, oras.DefaultCopyOptions) |
| 101 | + if err != nil { |
| 102 | + log.Errorf("copy: %v", err) |
| 103 | + return nil, fmt.Errorf("copy: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + manifestData, err := content.FetchAll(ctx, fs, manifestDescriptor) |
| 107 | + if err != nil { |
| 108 | + log.Errorf("fetch manifest: %v", err) |
| 109 | + return nil, fmt.Errorf("fetch manifest: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + if doTemplateFilesExistLocally(templateRootDir, remoteTemplate) { |
| 113 | + log.Debugf("Template %q already available in: %s\n", ociName, templateRootDir) |
| 114 | + } else { |
| 115 | + log.Infof("Pulling compage template %q\n", ociName) |
| 116 | + template, err = getCompageTemplateFromManifestLayers(manifestData, templateRootDir) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("get media types from layers: %w", err) |
| 119 | + } |
| 120 | + } |
| 121 | + log.Debugf("template successfully pulled in %s", templateRootDir) |
| 122 | + |
| 123 | + return template, nil |
| 124 | +} |
| 125 | + |
| 126 | +// getCompageTemplateFromManifestLayers returns the list of templates from an OCI manifest |
| 127 | +func getCompageTemplateFromManifestLayers(manifestData []byte, templateRootDir string) ( |
| 128 | + *string, error) { |
| 129 | + specification := spec.Manifest{} |
| 130 | + err := json.Unmarshal(manifestData, &specification) |
| 131 | + if err != nil { |
| 132 | + log.Errorf("unmarshal manifest: %v", err) |
| 133 | + return nil, fmt.Errorf("unmarshal manifest: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + for _, layer := range specification.Layers { |
| 137 | + switch layer.MediaType { |
| 138 | + case commonTemplatesMediaType: |
| 139 | + fallthrough |
| 140 | + case compageTemplateGoMediaType: |
| 141 | + if title, ok := layer.Annotations["org.opencontainers.image.title"]; ok && title != "" { |
| 142 | + template := filepath.Join(templateRootDir, title) |
| 143 | + return &template, nil |
| 144 | + } |
| 145 | + default: |
| 146 | + log.Warningf("unknown media type: %q\n", layer.MediaType) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return nil, nil |
| 151 | +} |
| 152 | + |
| 153 | +/* |
| 154 | +doTemplateFilesExistLocally returns true if the template folder/directory is already available locally. |
| 155 | +One more check is to see if the template has been tampered locally. |
| 156 | +*/ |
| 157 | +func doTemplateFilesExistLocally(templateRootDir string, template *string) bool { |
| 158 | + var errs []error |
| 159 | + templateRootDirFileInfo, err := os.Stat(templateRootDir) |
| 160 | + // If store path exist and is a directory then we do nothing |
| 161 | + if err == nil { |
| 162 | + if !templateRootDirFileInfo.IsDir() { |
| 163 | + errs = append(errs, fmt.Errorf("template root dir %s already exist and is not a directory", templateRootDir)) |
| 164 | + } |
| 165 | + } else { |
| 166 | + if !errors.Is(err, os.ErrNotExist) { |
| 167 | + errs = append(errs, fmt.Errorf("getting information about %s: %w", templateRootDir, err)) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + isFileExist := func(file *string) { |
| 172 | + _, err = os.Stat(*file) |
| 173 | + if err == nil { |
| 174 | + return |
| 175 | + } |
| 176 | + if errors.Is(err, os.ErrNotExist) { |
| 177 | + errs = append(errs, fmt.Errorf("%s does not exist locally", *file)) |
| 178 | + } else { |
| 179 | + errs = append(errs, fmt.Errorf("something went wrong while checking if %s exist: %w", *file, err)) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + isFileExist(template) |
| 184 | + |
| 185 | + if len(errs) > 0 { |
| 186 | + for i := range errs { |
| 187 | + log.Debugln(errs[i]) |
| 188 | + } |
| 189 | + return false |
| 190 | + } |
| 191 | + |
| 192 | + // check if the template has been tampered locally |
| 193 | + //todo: implement this |
| 194 | + return true |
| 195 | +} |
0 commit comments