@@ -23,6 +23,7 @@ type snap struct {
2323 snapDir string
2424 snapDataDir string
2525 snapCommonDir string
26+ capiPath string
2627 runCommand func (context.Context , ... string ) error
2728
2829 clusterTokensMu sync.Mutex
@@ -34,13 +35,18 @@ type snap struct {
3435 applyCNIBackoff time.Duration
3536}
3637
38+ const (
39+ defaultCAPIPath = "/capi"
40+ )
41+
3742// NewSnap creates a new interface with the MicroK8s snap.
3843// NewSnap accepts the $SNAP, $SNAP_DATA and $SNAP_COMMON, directories, and a number of options.
3944func NewSnap (snapDir , snapDataDir , snapCommonDir string , options ... func (s * snap )) Snap {
4045 s := & snap {
4146 snapDir : snapDir ,
4247 snapDataDir : snapDataDir ,
4348 snapCommonDir : snapCommonDir ,
49+ capiPath : defaultCAPIPath ,
4450 runCommand : util .RunCommand ,
4551 }
4652
@@ -65,6 +71,9 @@ func (s *snap) GetSnapDataPath(parts ...string) string {
6571func (s * snap ) GetSnapCommonPath (parts ... string ) string {
6672 return filepath .Join (append ([]string {s .snapCommonDir }, parts ... )... )
6773}
74+ func (s * snap ) GetCAPIPath (parts ... string ) string {
75+ return filepath .Join (append ([]string {s .capiPath }, parts ... )... )
76+ }
6877
6978func (s * snap ) GetGroupName () string {
7079 if s .isStrict () {
@@ -331,6 +340,15 @@ func (s *snap) GetKnownToken(username string) (string, error) {
331340 return "" , fmt .Errorf ("no known token found for user %s" , username )
332341}
333342
343+ // IsCAPIAuthTokenValid checks if the given CAPI auth token is valid.
344+ func (s * snap ) IsCAPIAuthTokenValid (token string ) (bool , error ) {
345+ contents , err := util .ReadFile (s .GetCAPIPath ("etc" , "token" ))
346+ if err != nil {
347+ return false , fmt .Errorf ("failed to read token file: %w" , err )
348+ }
349+ return strings .TrimSpace (contents ) == token , nil
350+ }
351+
334352func (s * snap ) SignCertificate (ctx context.Context , csrPEM []byte ) ([]byte , error ) {
335353 // TODO: consider using crypto/x509 for this instead of relying on openssl commands.
336354 // NOTE(neoaggelos): x509.CreateCertificate() has some hardcoded fields that are incompatible with MicroK8s.
0 commit comments