@@ -95,6 +95,7 @@ func EnsureCertificate(ctx context.Context, c client.Client, host string) error
9595 IsCA : false ,
9696 Usages : []cmapi.KeyUsage {
9797 cmapi .UsageServerAuth ,
98+ cmapi .UsageClientAuth ,
9899 cmapi .UsageCertSign ,
99100 cmapi .UsageDigitalSignature ,
100101 cmapi .UsageKeyEncipherment ,
@@ -125,81 +126,85 @@ func EnsureCertificate(ctx context.Context, c client.Client, host string) error
125126 return nil
126127}
127128
128- var secretToFileMap = map [string ]string {
129- "ca.crt" : "CA/cacert.pem" ,
130- "tls.crt" : "libvirt/servercert.pem" ,
131- "tls.key" : "libvirt/private/serverkey.pem" ,
129+ var secretToFileMap = map [string ][] string {
130+ "ca.crt" : { "CA/cacert.pem" , "qemu/ca-cert.pem" } ,
131+ "tls.crt" : { "libvirt/servercert.pem" , "qemu/server-cert.pem" } ,
132+ "tls.key" : { "libvirt/private/serverkey.pem" , "qemu/server-key.pem" } ,
132133}
133134
134- var symLinkMap = map [string ]string {
135- "servercert.pem" : "libvirt/clientcert .pem" ,
136- "serverkey.pem" : "libvirt/private/clientkey .pem" ,
135+ var symLinkMap = map [string ][] string {
136+ "servercert.pem" : { "libvirt/client-cert .pem" , "qemu/client-cert.pem" } ,
137+ "serverkey.pem" : { "libvirt/private/client-key .pem" , "qemu/client-key.pem" } ,
137138}
138139
139140func UpdateTLSCertificate (ctx context.Context , data map [string ][]byte ) error {
140141 log := logger .FromContext (ctx )
141142 log .Info ("updating TLS certificates for libvirt" , "path" , pki )
142143
143144 // write files
144- for source , target := range secretToFileMap {
145- // prepend the pki path for the target
146- target = filepath .Join (pki , target )
145+ for source , targets := range secretToFileMap {
146+ for _ , target := range targets {
147+ // prepend the pki path for the target
148+ target = filepath .Join (pki , target )
147149
148- if _ , ok := data [source ]; ! ok {
149- return fmt .Errorf ("missing data for secret key %s" , source )
150- }
150+ if _ , ok := data [source ]; ! ok {
151+ return fmt .Errorf ("missing data for secret key %s" , source )
152+ }
151153
152- // ensure the target directory exists
153- if err := os .MkdirAll (filepath .Dir (target ), 0755 ); err != nil {
154- return fmt .Errorf ("failed to create directory %s: %w" , filepath .Dir (target ), err )
155- }
154+ // ensure the target directory exists
155+ if err := os .MkdirAll (filepath .Dir (target ), 0755 ); err != nil {
156+ return fmt .Errorf ("failed to create directory %s: %w" , filepath .Dir (target ), err )
157+ }
156158
157- // write the file
158- if err := os .WriteFile (target , data [source ], 0640 ); err != nil {
159- return fmt .Errorf ("failed to write targetFile %s: %w" , target , err )
159+ // write the file
160+ if err := os .WriteFile (target , data [source ], 0640 ); err != nil {
161+ return fmt .Errorf ("failed to write targetFile %s: %w" , target , err )
162+ }
160163 }
161164 }
162165
163166 // handle symlinks
164- for source , target := range symLinkMap {
165- // prepend the pki path for both, source and target
166- target = filepath .Join (pki , target )
167-
168- // ensure the target directory exists
169- if err := os .MkdirAll (filepath .Dir (target ), 0755 ); err != nil {
170- return fmt .Errorf ("failed to create directory %s: %w" , filepath .Dir (target ), err )
171- }
172-
173- // check if the target exists and is correct, else create symlink
174- fileInfo , err := os .Lstat (target )
175- if err != nil {
176- if ! errors .Is (err , os .ErrNotExist ) {
177- return fmt .Errorf ("failed to stat target %s: %w" , target , err )
167+ for source , targets := range symLinkMap {
168+ for _ , target := range targets {
169+ // prepend the pki path for both, source and target
170+ target = filepath .Join (pki , target )
171+
172+ // ensure the target directory exists
173+ if err := os .MkdirAll (filepath .Dir (target ), 0755 ); err != nil {
174+ return fmt .Errorf ("failed to create directory %s: %w" , filepath .Dir (target ), err )
178175 }
179- } else {
180- // check if the target is a symlink, and correct it if necessary
181- if fileInfo .Mode ()& os .ModeSymlink != 0 {
182- // if the target is a symlink, check if it points to the correct source
183- link , err := os .Readlink (target )
184- if err != nil {
185- return fmt .Errorf ("failed to read symlink %s: %w" , target , err )
186- }
187176
188- // if the link is correctly pointing to the source, continue
189- if filepath .Clean (link ) == filepath .Clean (source ) {
190- continue
177+ // check if the target exists and is correct, else create symlink
178+ fileInfo , err := os .Lstat (target )
179+ if err != nil {
180+ if ! errors .Is (err , os .ErrNotExist ) {
181+ return fmt .Errorf ("failed to stat target %s: %w" , target , err )
191182 }
192-
193- // link is not pointing to the source, remove it
194- if err := os .Remove (target ); err != nil {
195- return fmt .Errorf ("failed to remove symlink %s: %w" , target , err )
183+ } else {
184+ // check if the target is a symlink, and correct it if necessary
185+ if fileInfo .Mode ()& os .ModeSymlink != 0 {
186+ // if the target is a symlink, check if it points to the correct source
187+ link , err := os .Readlink (target )
188+ if err != nil {
189+ return fmt .Errorf ("failed to read symlink %s: %w" , target , err )
190+ }
191+
192+ // if the link is correctly pointing to the source, continue
193+ if filepath .Clean (link ) == filepath .Clean (source ) {
194+ continue
195+ }
196+
197+ // link is not pointing to the source, remove it
198+ if err := os .Remove (target ); err != nil {
199+ return fmt .Errorf ("failed to remove symlink %s: %w" , target , err )
200+ }
196201 }
197202 }
198- }
199203
200- // create symlink
201- if err := os .Symlink (source , target ); err != nil {
202- return fmt .Errorf ("failed to create symlink %s -> %s: %w" , target , source , err )
204+ // create symlink
205+ if err := os .Symlink (source , target ); err != nil {
206+ return fmt .Errorf ("failed to create symlink %s -> %s: %w" , target , source , err )
207+ }
203208 }
204209 }
205210 return nil
0 commit comments