Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions internal/ocm-cli/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"

"sigs.k8s.io/yaml"
)
Expand All @@ -24,6 +25,10 @@ func Execute(ctx context.Context, commands []string, args []string, ocmConfig st

if ocmConfig != NoOcmConfig {
ocmArgs = append(ocmArgs, "--config", ocmConfig)

if err := verifyOCMConfig(ocmConfig); err != nil {
return fmt.Errorf("invalid OCM configuration: %w", err)
}
}

ocmArgs = append(ocmArgs, commands...)
Expand Down Expand Up @@ -122,6 +127,10 @@ func GetComponentVersion(ctx context.Context, componentReference string, ocmConf

if ocmConfig != NoOcmConfig {
ocmArgs = append(ocmArgs, "--config", ocmConfig)

if err := verifyOCMConfig(ocmConfig); err != nil {
return nil, fmt.Errorf("invalid OCM configuration: %w", err)
}
}

ocmArgs = append(ocmArgs, "get", "componentversion", "--output", "yaml", componentReference)
Expand All @@ -140,3 +149,52 @@ func GetComponentVersion(ctx context.Context, componentReference string, ocmConf

return &cv, nil
}

type OCMConfiguration struct {
Type string `json:"type"`
Configurations []TypedOCMConfigConfiguration `json:"configurations"`
}

type TypedOCMConfigConfiguration struct {
Type string `json:"type"`
Repositories []OCMConfigRepository `json:"repositories"`
}

type OCMConfigRepository struct {
Repository *TypedOCMConfigRepository `json:"repository"`
}

type TypedOCMConfigRepository struct {
Type string `json:"type"`
}

const (
DockerConfigRepositoryType = "DockerConfig"
)

// verifyOCMConfig checks if the OCM configuration file exists and does not contain unsupported configuration.
func verifyOCMConfig(ocmConfig string) error {
if _, err := os.Stat(ocmConfig); os.IsNotExist(err) {
return fmt.Errorf("OCM configuration file does not exist: %s", ocmConfig)
}

file, err := os.ReadFile(ocmConfig)
if err != nil {
return fmt.Errorf("error reading OCM configuration file: %w", err)
}

var config OCMConfiguration
if err = yaml.Unmarshal(file, &config); err != nil {
return fmt.Errorf("error unmarshalling OCM configuration file: %w", err)
}

for _, typedConfig := range config.Configurations {
for _, repo := range typedConfig.Repositories {
if repo.Repository != nil && strings.Contains(repo.Repository.Type, DockerConfigRepositoryType) {
return fmt.Errorf("unsupported repository type: %s", repo.Repository.Type)
}
}
}

return nil
}
14 changes: 14 additions & 0 deletions internal/ocm-cli/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func TestExecute(t *testing.T) {
ocmConfig: "./testdata/ocm-config.yaml",
expectedError: nil,
},

{
desc: "get componentversion with unsupported ocm config",
commands: []string{"get", "componentversion"},
arguments: []string{"--output", "yaml", ctfIn},
ocmConfig: "./testdata/unsupported-ocm-config.yaml",
expectedError: expectError,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -114,6 +122,12 @@ func TestGetComponentVersion(t *testing.T) {
ocmConfig: "./testdata/ocm-config.yaml",
expectedError: nil,
},
{
desc: "get component version with unsupported ocm config",
componentRef: ctfIn,
ocmConfig: "./testdata/unsupported-ocm-config.yaml",
expectedError: expectError,
},
{
desc: "get component version with invalid reference",
componentRef: "invalid-component-ref",
Expand Down
15 changes: 11 additions & 4 deletions internal/ocm-cli/testdata/ocm-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
type: generic.config.ocm.software/v1
configurations:
- type: credentials.config.ocm.software
repositories:
- repository:
type: DockerConfig/v1
dockerConfigFile: "~/.docker/config.json"
consumers:
- identity:
type: OCIRegistry
scheme: https
hostname: ghcr.io
pathprefix: openmcp-project
credentials:
- type: Credentials
properties:
username: some-user
password: some-token
7 changes: 7 additions & 0 deletions internal/ocm-cli/testdata/unsupported-ocm-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: generic.config.ocm.software/v1
configurations:
- type: credentials.config.ocm.software
repositories:
- repository:
type: DockerConfig/v1
dockerConfigFile: "~/.docker/config.json"