Skip to content

Commit 16e4000

Browse files
committed
feat: check for unsupported OCM configuration type DockerConfig
1 parent c7a896e commit 16e4000

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

internal/ocm-cli/ocm.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"strings"
89

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

2526
if ocmConfig != NoOcmConfig {
2627
ocmArgs = append(ocmArgs, "--config", ocmConfig)
28+
29+
if err := verifyOCMConfig(ocmConfig); err != nil {
30+
return fmt.Errorf("invalid OCM configuration: %w", err)
31+
}
2732
}
2833

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

123128
if ocmConfig != NoOcmConfig {
124129
ocmArgs = append(ocmArgs, "--config", ocmConfig)
130+
131+
if err := verifyOCMConfig(ocmConfig); err != nil {
132+
return nil, fmt.Errorf("invalid OCM configuration: %w", err)
133+
}
125134
}
126135

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

141150
return &cv, nil
142151
}
152+
153+
type OCMConfiguration struct {
154+
Type string `json:"type"`
155+
Configurations []TypedOCMConfigConfiguration `json:"configurations"`
156+
}
157+
158+
type TypedOCMConfigConfiguration struct {
159+
Type string `json:"type"`
160+
Repositories []OCMConfigRepository `json:"repositories"`
161+
}
162+
163+
type OCMConfigRepository struct {
164+
Repository *TypedOCMConfigRepository `json:"repository"`
165+
}
166+
167+
type TypedOCMConfigRepository struct {
168+
Type string `json:"type"`
169+
}
170+
171+
const (
172+
DockerConfigRepositoryType = "DockerConfig"
173+
)
174+
175+
// verifyOCMConfig checks if the OCM configuration file exists and does not contain unsupported configuration.
176+
func verifyOCMConfig(ocmConfig string) error {
177+
if _, err := os.Stat(ocmConfig); os.IsNotExist(err) {
178+
return fmt.Errorf("OCM configuration file does not exist: %s", ocmConfig)
179+
}
180+
181+
file, err := os.ReadFile(ocmConfig)
182+
if err != nil {
183+
return fmt.Errorf("error reading OCM configuration file: %w", err)
184+
}
185+
186+
var config OCMConfiguration
187+
if err = yaml.Unmarshal(file, &config); err != nil {
188+
return fmt.Errorf("error unmarshalling OCM configuration file: %w", err)
189+
}
190+
191+
for _, typedConfig := range config.Configurations {
192+
for _, repo := range typedConfig.Repositories {
193+
if repo.Repository != nil && strings.Contains(repo.Repository.Type, DockerConfigRepositoryType) {
194+
return fmt.Errorf("unsupported repository type: %s", repo.Repository.Type)
195+
}
196+
}
197+
}
198+
199+
return nil
200+
}

internal/ocm-cli/ocm_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ func TestExecute(t *testing.T) {
4646
ocmConfig: "./testdata/ocm-config.yaml",
4747
expectedError: nil,
4848
},
49+
50+
{
51+
desc: "get componentversion with unsupported ocm config",
52+
commands: []string{"get", "componentversion"},
53+
arguments: []string{"--output", "yaml", ctfIn},
54+
ocmConfig: "./testdata/unsupported-ocm-config.yaml",
55+
expectedError: expectError,
56+
},
4957
}
5058

5159
for _, tc := range testCases {
@@ -114,6 +122,12 @@ func TestGetComponentVersion(t *testing.T) {
114122
ocmConfig: "./testdata/ocm-config.yaml",
115123
expectedError: nil,
116124
},
125+
{
126+
desc: "get component version with unsupported ocm config",
127+
componentRef: ctfIn,
128+
ocmConfig: "./testdata/unsupported-ocm-config.yaml",
129+
expectedError: expectError,
130+
},
117131
{
118132
desc: "get component version with invalid reference",
119133
componentRef: "invalid-component-ref",
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
type: generic.config.ocm.software/v1
22
configurations:
33
- type: credentials.config.ocm.software
4-
repositories:
5-
- repository:
6-
type: DockerConfig/v1
7-
dockerConfigFile: "~/.docker/config.json"
4+
consumers:
5+
- identity:
6+
type: OCIRegistry
7+
scheme: https
8+
hostname: ghcr.io
9+
pathprefix: openmcp-project
10+
credentials:
11+
- type: Credentials
12+
properties:
13+
username: some-user
14+
password: some-token
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type: generic.config.ocm.software/v1
2+
configurations:
3+
- type: credentials.config.ocm.software
4+
repositories:
5+
- repository:
6+
type: DockerConfig/v1
7+
dockerConfigFile: "~/.docker/config.json"

0 commit comments

Comments
 (0)