in internal/cssc/cssc.go [73:120]
func GetFilterFromFilterPolicy(ctx context.Context, filterPolicy string, loginURL string, username string, password string) (Filter, error) {
filterPolicyPattern := `^[^:]+:[^:]+$`
re := regexp.MustCompile(filterPolicyPattern)
if !re.MatchString(filterPolicy) {
return Filter{}, errors.New("filter-policy should be in the format repository:tag e.g. continuouspatchpolicy:latest")
}
repoTag := strings.Split(filterPolicy, ":")
filterRepoName := repoTag[0]
filterRepoTagName := repoTag[1]
// Connect to the remote repository
repo, err := remote.NewRepository(fmt.Sprintf("%s/%s", loginURL, filterRepoName))
if err != nil {
return Filter{}, errors.Wrap(err, "error connecting to the repository when reading the filter policy")
}
repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.NewCache(),
Credential: auth.StaticCredential(loginURL, auth.Credential{
Username: username,
Password: password,
}),
}
// Get manifest and read content
_, pulledManifestContent, err := oras.FetchBytes(ctx, repo, filterRepoTagName, oras.DefaultFetchBytesOptions)
if err != nil {
return Filter{}, errors.Wrap(err, "error fetching filter manifest content when reading the filter policy")
}
var pulledManifest v1.Manifest
if err := json.Unmarshal(pulledManifestContent, &pulledManifest); err != nil {
return Filter{}, errors.Wrap(err, "error unmarshalling filter manifest content when reading the filter policy")
}
var fileContent []byte
for _, layer := range pulledManifest.Layers {
fileContent, err = content.FetchAll(ctx, repo, layer)
if err != nil {
return Filter{}, errors.Wrap(err, "error fetching filter content when reading the filter policy")
}
}
// Unmarshal the JSON file data to Filter struct
var filter = Filter{}
if err := json.Unmarshal(fileContent, &filter); err != nil {
return Filter{}, errors.Wrap(err, "error unmarshalling json content when reading the filter policy")
}
return filter, nil
}