mirror of https://github.com/hashicorp/consul
Mitchell Hashimoto
7 years ago
4 changed files with 188 additions and 0 deletions
@ -0,0 +1,65 @@
|
||||
package api |
||||
|
||||
import ( |
||||
"time" |
||||
) |
||||
|
||||
// CARootList is the structure for the results of listing roots.
|
||||
type CARootList struct { |
||||
ActiveRootID string |
||||
Roots []*CARoot |
||||
} |
||||
|
||||
// CARoot is a single CA within Connect.
|
||||
type CARoot struct { |
||||
ID string |
||||
Name string |
||||
RootCert string |
||||
Active bool |
||||
CreateIndex uint64 |
||||
ModifyIndex uint64 |
||||
} |
||||
|
||||
type IssuedCert struct { |
||||
SerialNumber string |
||||
CertPEM string |
||||
PrivateKeyPEM string |
||||
Service string |
||||
ServiceURI string |
||||
ValidAfter time.Time |
||||
ValidBefore time.Time |
||||
CreateIndex uint64 |
||||
ModifyIndex uint64 |
||||
} |
||||
|
||||
// Connect can be used to work with endpoints related to Connect, the
|
||||
// feature for securely connecting services within Consul.
|
||||
type Connect struct { |
||||
c *Client |
||||
} |
||||
|
||||
// Health returns a handle to the health endpoints
|
||||
func (c *Client) Connect() *Connect { |
||||
return &Connect{c} |
||||
} |
||||
|
||||
// CARoots queries the list of available roots.
|
||||
func (h *Connect) CARoots(q *QueryOptions) (*CARootList, *QueryMeta, error) { |
||||
r := h.c.newRequest("GET", "/v1/connect/ca/roots") |
||||
r.setQueryOptions(q) |
||||
rtt, resp, err := requireOK(h.c.doRequest(r)) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
defer resp.Body.Close() |
||||
|
||||
qm := &QueryMeta{} |
||||
parseQueryMeta(resp, qm) |
||||
qm.RequestTime = rtt |
||||
|
||||
var out CARootList |
||||
if err := decodeBody(resp, &out); err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return &out, qm, nil |
||||
} |
@ -0,0 +1,26 @@
|
||||
package api |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
// NOTE(mitchellh): we don't have a way to test CA roots yet since there
|
||||
// is no API public way to configure the root certs. This wll be resolved
|
||||
// in the future and we can write tests then. This is tested in agent and
|
||||
// agent/consul which do have internal access to manually create roots.
|
||||
|
||||
func TestAPI_ConnectCARoots_empty(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
require := require.New(t) |
||||
c, s := makeClient(t) |
||||
defer s.Stop() |
||||
|
||||
connect := c.Connect() |
||||
list, meta, err := connect.CARoots(nil) |
||||
require.Nil(err) |
||||
require.Equal(uint64(0), meta.LastIndex) |
||||
require.Len(list.Roots, 0) |
||||
} |
Loading…
Reference in new issue