@@ -25,6 +25,7 @@ import (
2525 "github.com/uyuni-project/uyuni-tools/shared/podman"
2626 "github.com/uyuni-project/uyuni-tools/shared/types"
2727 shared_utils "github.com/uyuni-project/uyuni-tools/shared/utils"
28+ "gopkg.in/yaml.v2"
2829)
2930
3031const (
@@ -44,6 +45,7 @@ const (
4445var contextRunner = shared_utils .NewRunnerWithContext
4546var newRunner = shared_utils .NewRunner
4647var systemdGenerator func (shared_utils.Template , string , string , string ) error = generateSystemdFile
48+ var createSecret = podman .CreateSecret
4749
4850// PodmanProxyFlags are the flags used by podman proxy install and upgrade command.
4951type PodmanProxyFlags struct {
@@ -86,6 +88,15 @@ func GenerateSystemdService(
8688 if podman .HasSecret (SystemIDSecret ) {
8789 dataHttpd .SystemIDSecret = SystemIDSecret
8890 }
91+ if podman .HasSecret (podman .CASecret ) {
92+ dataHttpd .CaSecret = podman .CASecret
93+ }
94+ if podman .HasSecret (podman .ProxySSLCertSecret ) {
95+ dataHttpd .CertSecret = podman .ProxySSLCertSecret
96+ }
97+ if podman .HasSecret (podman .ProxySSLKeySecret ) {
98+ dataHttpd .KeySecret = podman .ProxySSLKeySecret
99+ }
89100
90101 additionHttpdTuningSettings := ""
91102 additionHTTPConfPath , err := getPathOrDefault (flags .Tuning .Httpd , defaultApacheConf )
@@ -159,6 +170,9 @@ func GenerateSystemdService(
159170 Volumes : shared_utils .ProxyTftpdVolumes ,
160171 HTTPProxyFile : httpProxyConfig ,
161172 }
173+ if podman .HasSecret (podman .CASecret ) {
174+ dataTftpd .CaSecret = podman .CASecret
175+ }
162176 if err := systemdGenerator (dataTftpd , "tftpd" , tftpdImage , "" ); err != nil {
163177 return err
164178 }
@@ -308,6 +322,10 @@ func Upgrade(
308322 return err
309323 }
310324
325+ if err := ExtractSecrets (); err != nil {
326+ return err
327+ }
328+
311329 hostData , err := podman .InspectHost ()
312330 if err != nil {
313331 return err
@@ -450,5 +468,114 @@ func GetSystemID() error {
450468 }
451469 log .Trace ().Msgf ("SystemID: %s" , systemid )
452470
453- return podman .CreateSecret (SystemIDSecret , systemid )
471+ return createSecret (SystemIDSecret , systemid )
472+ }
473+
474+ var proxyConfigDir = "/etc/uyuni/proxy"
475+
476+ // ExtractSecrets extracts certificates from YAML files and creates podman secrets.
477+ func ExtractSecrets () error {
478+ if err := extractCASecret (path .Join (proxyConfigDir , "config.yaml" )); err != nil {
479+ return err
480+ }
481+ return extractHttpdSecrets (path .Join (proxyConfigDir , "httpd.yaml" ))
482+ }
483+
484+ func extractCASecret (configPath string ) error {
485+ config , err := loadYaml (configPath )
486+ if err != nil || config == nil {
487+ return err
488+ }
489+
490+ updated , err := extractKeyToSecret (config , "ca_crt" , podman .CASecret )
491+ if err != nil {
492+ return err
493+ }
494+
495+ if updated {
496+ return saveYaml (configPath , config )
497+ }
498+ return nil
499+ }
500+
501+ func extractHttpdSecrets (httpdPath string ) error {
502+ config , err := loadYaml (httpdPath )
503+ if err != nil || config == nil {
504+ return err
505+ }
506+
507+ httpd , ok := config ["httpd" ]
508+ if ! ok {
509+ return nil
510+ }
511+
512+ httpdMap := ensureStringMap (httpd )
513+ if httpdMap == nil {
514+ return nil
515+ }
516+ config ["httpd" ] = httpdMap
517+
518+ updated1 , err := extractKeyToSecret (httpdMap , "server_crt" , podman .ProxySSLCertSecret )
519+ if err != nil {
520+ return err
521+ }
522+ updated2 , err := extractKeyToSecret (httpdMap , "server_key" , podman .ProxySSLKeySecret )
523+ if err != nil {
524+ return err
525+ }
526+
527+ if updated1 || updated2 {
528+ return saveYaml (httpdPath , config )
529+ }
530+ return nil
531+ }
532+
533+ func loadYaml (filePath string ) (map [string ]interface {}, error ) {
534+ if ! shared_utils .FileExists (filePath ) {
535+ return nil , nil
536+ }
537+ data , err := os .ReadFile (filePath )
538+ if err != nil {
539+ return nil , shared_utils .Errorf (err , L ("failed to read %s" ), filePath )
540+ }
541+ var config map [string ]interface {}
542+ if err := yaml .Unmarshal (data , & config ); err != nil {
543+ return nil , shared_utils .Errorf (err , L ("failed to unmarshal %s" ), filePath )
544+ }
545+ return config , nil
546+ }
547+
548+ func saveYaml (filePath string , config map [string ]interface {}) error {
549+ newData , err := yaml .Marshal (config )
550+ if err != nil {
551+ return shared_utils .Errorf (err , L ("failed to marshal %s" ), filePath )
552+ }
553+ if err := os .WriteFile (filePath , newData , 0644 ); err != nil {
554+ return shared_utils .Errorf (err , L ("failed to write %s" ), filePath )
555+ }
556+ return nil
557+ }
558+
559+ func extractKeyToSecret (m map [string ]interface {}, key string , secretName string ) (bool , error ) {
560+ if val , ok := m [key ].(string ); ok && val != "" {
561+ if err := createSecret (secretName , val ); err != nil {
562+ return false , shared_utils .Errorf (err , L ("failed to create %s secret" ), secretName )
563+ }
564+ delete (m , key )
565+ return true , nil
566+ }
567+ return false , nil
568+ }
569+
570+ func ensureStringMap (data interface {}) map [string ]interface {} {
571+ if m , ok := data .(map [string ]interface {}); ok {
572+ return m
573+ } else if m , ok := data .(map [interface {}]interface {}); ok {
574+ res := make (map [string ]interface {})
575+ for k , v := range m {
576+ res [fmt .Sprintf ("%v" , k )] = v
577+ }
578+ return res
579+ }
580+ return nil
454581}
0 commit comments