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+ }
0 commit comments