-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra SANs to microcluster certificate (#629)
--------- Co-authored-by: Berkay Tekin Öz <berkay.tekinoz@canonical.com>
- Loading branch information
1 parent
6d8b29d
commit 991e48e
Showing
6 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/canonical/k8s/pkg/utils" | ||
"github.com/canonical/lxd/shared" | ||
microclusterTypes "github.com/canonical/microcluster/v3/rest/types" | ||
"github.com/canonical/microcluster/v3/state" | ||
) | ||
|
||
// onPreInit is called before we bootstrap or join a node. | ||
func (a *App) onPreInit(ctx context.Context, s state.State, bootstrap bool, initConfig map[string]string) error { | ||
if bootstrap { | ||
return nil | ||
} | ||
|
||
controlPlaneJoinConfig, err := utils.MicroclusterControlPlaneJoinConfigFromMap(initConfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to get control plane join config, boostrap %v: %w", bootstrap, err) | ||
} | ||
extraSANs := controlPlaneJoinConfig.ExtraSANS | ||
|
||
if err := os.Remove(filepath.Join(s.FileSystem().StateDir, "server.crt")); err != nil { | ||
return fmt.Errorf("failed to remove server.crt: %w", err) | ||
} | ||
|
||
if err := os.Remove(filepath.Join(s.FileSystem().StateDir, "server.key")); err != nil { | ||
return fmt.Errorf("failed to remove server.key: %w", err) | ||
} | ||
|
||
cert, err := shared.KeyPairAndCA( | ||
s.FileSystem().StateDir, | ||
string(microclusterTypes.ServerCertificateName), | ||
shared.CertServer, | ||
shared.CertOptions{ | ||
AddHosts: true, | ||
CommonName: s.Name(), | ||
SubjectAlternativeNames: extraSANs, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := a.client.UpdateCertificate(ctx, microclusterTypes.ServerCertificateName, microclusterTypes.KeyPair{ | ||
Cert: string(cert.PublicKey()), | ||
Key: string(cert.PrivateKey()), | ||
}); err != nil { | ||
return fmt.Errorf("failed to update certificate %s: %w", microclusterTypes.ServerCertificateName, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters