From 7457bf31366ce0873e38fbebed8af66e714a5231 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Thu, 3 Feb 2022 15:10:31 +0100 Subject: [PATCH] kes: add Status API to client SDK This commit adds the `Status` API call to the client SDK. The current client API only contains a subset of the status information exposed by the server due to API instability. Signed-off-by: Andreas Auernhammer --- client.go | 23 +++++++++++++++++++++++ integration_test.go | 18 ++++++++++++++++++ status.go | 14 ++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 status.go diff --git a/client.go b/client.go index b5319e8b..301e340a 100644 --- a/client.go +++ b/client.go @@ -139,6 +139,29 @@ func (c *Client) Version(ctx context.Context) (string, error) { return response.Version, nil } +// Status returns the current state of the KES server. +func (c *Client) Status(ctx context.Context) (State, error) { + client := retry(c.HTTPClient) + resp, err := client.Send(ctx, http.MethodGet, c.Endpoints, "/v1/status", nil) + if err != nil { + return State{}, err + } + if resp.StatusCode != http.StatusOK { + return State{}, parseErrorResponse(resp) + } + + type Response struct { + Version string `json:"version"` + UpTime time.Duration `json:"uptime"` + } + const limit = 1 << 20 + var response Response + if err = json.NewDecoder(io.LimitReader(resp.Body, limit)).Decode(&response); err != nil { + return State{}, err + } + return State(response), nil +} + // CreateKey creates a new cryptographic key. The key will // be generated by the KES server. // diff --git a/integration_test.go b/integration_test.go index 94e73789..b0e25a84 100644 --- a/integration_test.go +++ b/integration_test.go @@ -68,6 +68,24 @@ func TestVersion(t *testing.T) { } } +func TestStatus(t *testing.T) { + if !*IsIntegrationTest { + t.SkipNow() + } + + client, err := newClient() + if err != nil { + t.Fatalf("Failed to create KES client: %v", err) + } + state, err := client.Status(context.Background()) + if err != nil { + t.Fatalf("Failed to fetch KES server state: %v", err) + } + if state.UpTime == 0 { + t.Fatal("The KES server up time cannot be 0") + } +} + func TestCreateKey(t *testing.T) { if !*IsIntegrationTest { t.SkipNow() diff --git a/status.go b/status.go new file mode 100644 index 00000000..4861e33a --- /dev/null +++ b/status.go @@ -0,0 +1,14 @@ +// Copyright 2022 - MinIO, Inc. All rights reserved. +// Use of this source code is governed by the AGPLv3 +// license that can be found in the LICENSE file. + +package kes + +import "time" + +// State is a KES server status snapshot. +type State struct { + Version string // The KES server version + + UpTime time.Duration // The time the KES server has been up and running +}