@@ -19,7 +19,7 @@ import (
1919)
2020
2121type loadAssetFunc func (chart , asset string ) ([]byte , error )
22- type checkAssetFunc func (regClient * registry.Client , ociDNS , chart , version string ) (bool , error )
22+ type checkAssetFunc func (ctx context. Context , regClient * registry.Client , ociDNS , chart , version string ) (bool , error )
2323type pushFunc func (helmClient * registry.Client , data []byte , url string ) error
2424
2525type oci struct {
@@ -39,7 +39,7 @@ func UpdateOCI(ctx context.Context, rootFs billy.Filesystem, ociDNS, ociUser, oc
3939 return err
4040 }
4141
42- oci , err := setupOCI (ociDNS , ociUser , ociPass , debug )
42+ oci , err := setupOCI (ctx , ociDNS , ociUser , ociPass , debug )
4343 if err != nil {
4444 return err
4545 }
@@ -53,15 +53,15 @@ func UpdateOCI(ctx context.Context, rootFs billy.Filesystem, ociDNS, ociUser, oc
5353 return nil
5454}
5555
56- func setupOCI (ociDNS , ociUser , ociPass string , debug bool ) (* oci , error ) {
56+ func setupOCI (ctx context. Context , ociDNS , ociUser , ociPass string , debug bool ) (* oci , error ) {
5757 var err error
5858 o := & oci {
5959 DNS : ociDNS ,
6060 user : ociUser ,
6161 password : ociPass ,
6262 }
6363
64- o .helmClient , err = setupHelm (o .DNS , o .user , o .password , debug )
64+ o .helmClient , err = setupHelm (ctx , o .DNS , o .user , o .password , debug )
6565 if err != nil {
6666 return nil , err
6767 }
@@ -73,7 +73,7 @@ func setupOCI(ociDNS, ociUser, ociPass string, debug bool) (*oci, error) {
7373 return o , nil
7474}
7575
76- func setupHelm (ociDNS , ociUser , ociPass string , debug bool ) (* registry.Client , error ) {
76+ func setupHelm (ctx context. Context , ociDNS , ociUser , ociPass string , debug bool ) (* registry.Client , error ) {
7777 settings := cli .New ()
7878 actionConfig := new (action.Configuration )
7979 if err := actionConfig .Init (settings .RESTClientGetter (), settings .Namespace (), os .Getenv ("HELM_DRIVER" ), func (format string , v ... interface {}) {
@@ -83,42 +83,73 @@ func setupHelm(ociDNS, ociUser, ociPass string, debug bool) (*registry.Client, e
8383 }
8484
8585 var regClient * registry.Client
86+ var err error
8687
87- if debug {
88- fmt .Println ("debug mode you need to provide a self-signed certificate" )
89- caFile := "/etc/docker/certs.d/" + ociDNS + "/ca.crt"
88+ registryHost := extractRegistryHost (ociDNS )
89+ isLocalHost := strings .HasPrefix (registryHost , "localhost:" )
9090
91- regClient , err := registry .NewRegistryClientWithTLS (os .Stdout , "" , "" , caFile , false , "" , true )
91+ switch {
92+ // Debug Mode but pointing to a server with custom-certificates
93+ case debug && ! isLocalHost :
94+ logger .Log (ctx , slog .LevelDebug , "debug mode" , slog .Bool ("localhost" , isLocalHost ))
95+ caFile := "/etc/docker/certs.d/" + registryHost + "/ca.crt"
96+ regClient , err = registry .NewRegistryClientWithTLS (os .Stdout , "" , "" , caFile , false , "" , true )
9297 if err != nil {
98+ logger .Log (ctx , slog .LevelError , "failed to create registry client with TLS" )
9399 return nil , err
94100 }
95-
96- if err := regClient .Login (
97- ociDNS ,
101+ if err = regClient .Login (
102+ registryHost ,
98103 registry .LoginOptInsecure (false ),
99104 registry .LoginOptTLSClientConfig ("" , "" , caFile ),
100105 registry .LoginOptBasicAuth (ociUser , ociPass ),
101106 ); err != nil {
107+ logger .Log (ctx , slog .LevelError , "failed to login to registry with TLS" , slog .Group (ociDNS , ociUser , ociPass ))
102108 return nil , err
103109 }
104110
105- return regClient , nil
106- }
107-
108- regClient , err := registry .NewClient (registry .ClientOptDebug (false ))
109- if err != nil {
110- return nil , err
111- }
111+ // Debug Mode at localhost without TLS
112+ case debug && isLocalHost :
113+ logger .Log (ctx , slog .LevelDebug , "debug mode" , slog .Bool ("localhost" , isLocalHost ))
114+ regClient , err = registry .NewClient (
115+ registry .ClientOptDebug (true ),
116+ registry .ClientOptPlainHTTP (),
117+ )
118+ if err != nil {
119+ logger .Log (ctx , slog .LevelError , "failed to create registry client" )
120+ return nil , err
121+ }
122+ if err = regClient .Login (registryHost ,
123+ registry .LoginOptInsecure (true ), // true for localhost, false for production
124+ registry .LoginOptBasicAuth (ociUser , ociPass )); err != nil {
125+ logger .Log (ctx , slog .LevelError , "failed to login to registry" , slog .Group (ociDNS , ociUser , ociPass ))
126+ return nil , err
127+ }
112128
113- if err := regClient .Login (ociDNS ,
114- registry .LoginOptInsecure (false ),
115- registry .LoginOptBasicAuth (ociUser , ociPass )); err != nil {
116- return nil , err
129+ // Production code with Secure Mode and authentication
130+ default :
131+ regClient , err = registry .NewClient (registry .ClientOptDebug (false ))
132+ if err != nil {
133+ return nil , err
134+ }
135+ if err = regClient .Login (registryHost ,
136+ registry .LoginOptInsecure (false ),
137+ registry .LoginOptBasicAuth (ociUser , ociPass )); err != nil {
138+ return nil , err
139+ }
117140 }
118141
119142 return regClient , nil
120143}
121144
145+ // extractRegistryHost will extract the DNS for login
146+ func extractRegistryHost (ociDNS string ) string {
147+ if idx := strings .Index (ociDNS , "/" ); idx != - 1 {
148+ return ociDNS [:idx ]
149+ }
150+ return ociDNS
151+ }
152+
122153// update will attempt to update a helm chart to an OCI registry.
123154// 2 phases:
124155// - 1: Pre-Flight validations (check the current chart + check if it already exists)
@@ -148,7 +179,7 @@ func (o *oci) update(ctx context.Context, release *options.ReleaseOptions) ([]st
148179
149180 // Check if the asset version already exists in the OCI registry
150181 // Never overwrite a previously released chart!
151- exists , err := o .checkAsset (o .helmClient , o .DNS , chart , version )
182+ exists , err := o .checkAsset (ctx , o .helmClient , o .DNS , chart , version )
152183 if err != nil {
153184 logger .Log (ctx , slog .LevelError , "failed to check registry for asset" , slog .String ("asset" , asset ))
154185 return pushedAssets , err
@@ -221,14 +252,16 @@ func buildPushURL(ociDNS, chart, version string) string {
221252}
222253
223254// checkAsset checks if a specific asset version exists in the OCI registry
224- func checkAsset (helmClient * registry.Client , ociDNS , chart , version string ) (bool , error ) {
255+ func checkAsset (ctx context. Context , helmClient * registry.Client , ociDNS , chart , version string ) (bool , error ) {
225256 // Once issue is resolved: https://github.com/helm/helm/issues/13368
226257 // Replace by: helmClient.Tags(ociDNS + "/" + chart + ":" + version)
227258 existingVersions , err := helmClient .Tags (ociDNS + "/" + chart )
228259 if err != nil {
229260 if strings .Contains (err .Error (), "unexpected status code 404: name unknown: repository name not known to registry" ) {
261+ logger .Log (ctx , slog .LevelDebug , "asset does not exist at registry" , slog .String ("chart" , chart ))
230262 return false , nil
231263 }
264+ logger .Err (err )
232265 return false , err
233266 }
234267
0 commit comments