-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add certificate expiry endpoint (#683)
- Loading branch information
1 parent
cfa7f99
commit d189816
Showing
6 changed files
with
95 additions
and
4 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,50 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
apiv1 "github.com/canonical/k8s-snap-api/api/v1" | ||
databaseutil "github.com/canonical/k8s/pkg/k8sd/database/util" | ||
pkiutil "github.com/canonical/k8s/pkg/utils/pki" | ||
"github.com/canonical/lxd/lxd/response" | ||
"github.com/canonical/microcluster/v3/state" | ||
) | ||
|
||
func (e *Endpoints) postCertificatesExpiry(s state.State, r *http.Request) response.Response { | ||
config, err := databaseutil.GetClusterConfig(r.Context(), s) | ||
if err != nil { | ||
return response.InternalError(fmt.Errorf("failed to get cluster config: %w", err)) | ||
} | ||
|
||
certificates := []string{ | ||
config.Certificates.GetCACert(), | ||
config.Certificates.GetClientCACert(), | ||
config.Certificates.GetAdminClientCert(), | ||
config.Certificates.GetAPIServerKubeletClientCert(), | ||
config.Certificates.GetFrontProxyCACert(), | ||
} | ||
|
||
var earliestExpiry time.Time | ||
// Find the earliest expiry certificate | ||
// They should all be about the same but better double-check this. | ||
for _, cert := range certificates { | ||
if cert == "" { | ||
continue | ||
} | ||
|
||
cert, _, err := pkiutil.LoadCertificate(cert, "") | ||
if err != nil { | ||
return response.InternalError(fmt.Errorf("failed to load certificate: %w", err)) | ||
} | ||
|
||
if earliestExpiry.IsZero() || cert.NotAfter.Before(earliestExpiry) { | ||
earliestExpiry = cert.NotAfter | ||
} | ||
} | ||
|
||
return response.SyncResponse(true, &apiv1.CertificatesExpiryResponse{ | ||
ExpiryDate: earliestExpiry.Format(time.RFC3339), | ||
}) | ||
} |
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