2018-03-28 04:33:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-06-27 05:40:06 +00:00
|
|
|
"bytes"
|
2018-05-12 02:47:26 +00:00
|
|
|
"fmt"
|
2018-06-27 05:40:06 +00:00
|
|
|
"io"
|
2018-03-28 04:33:05 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Intention defines an intention for the Connect Service Graph. This defines
|
|
|
|
// the allowed or denied behavior of a connection between two services using
|
|
|
|
// Connect.
|
|
|
|
type Intention struct {
|
|
|
|
// ID is the UUID-based ID for the intention, always generated by Consul.
|
2020-10-06 18:24:05 +00:00
|
|
|
ID string `json:",omitempty"`
|
2018-03-28 04:33:05 +00:00
|
|
|
|
|
|
|
// Description is a human-friendly description of this intention.
|
|
|
|
// It is opaque to Consul and is only stored and transferred in API
|
|
|
|
// requests.
|
2020-10-06 18:24:05 +00:00
|
|
|
Description string `json:",omitempty"`
|
2018-03-28 04:33:05 +00:00
|
|
|
|
|
|
|
// SourceNS, SourceName are the namespace and name, respectively, of
|
|
|
|
// the source service. Either of these may be the wildcard "*", but only
|
|
|
|
// the full value can be a wildcard. Partial wildcards are not allowed.
|
|
|
|
// The source may also be a non-Consul service, as specified by SourceType.
|
|
|
|
//
|
|
|
|
// DestinationNS, DestinationName is the same, but for the destination
|
|
|
|
// service. The same rules apply. The destination is always a Consul
|
|
|
|
// service.
|
|
|
|
SourceNS, SourceName string
|
|
|
|
DestinationNS, DestinationName string
|
|
|
|
|
|
|
|
// SourceType is the type of the value for the source.
|
|
|
|
SourceType IntentionSourceType
|
|
|
|
|
2020-05-29 18:19:16 +00:00
|
|
|
// Action is whether this is an allowlist or denylist intention.
|
2020-10-06 22:09:13 +00:00
|
|
|
Action IntentionAction `json:",omitempty"`
|
|
|
|
|
|
|
|
// Permissions is the list of additional L7 attributes that extend the
|
|
|
|
// intention definition.
|
|
|
|
//
|
|
|
|
// NOTE: This field is not editable unless editing the underlying
|
|
|
|
// service-intentions config entry directly.
|
|
|
|
Permissions []*IntentionPermission `json:",omitempty"`
|
2018-03-28 04:33:05 +00:00
|
|
|
|
2020-05-15 01:10:03 +00:00
|
|
|
// DefaultAddr is not used.
|
|
|
|
// Deprecated: DefaultAddr is not used and may be removed in a future version.
|
|
|
|
DefaultAddr string `json:",omitempty"`
|
|
|
|
// DefaultPort is not used.
|
|
|
|
// Deprecated: DefaultPort is not used and may be removed in a future version.
|
|
|
|
DefaultPort int `json:",omitempty"`
|
2018-03-28 04:33:05 +00:00
|
|
|
|
|
|
|
// Meta is arbitrary metadata associated with the intention. This is
|
|
|
|
// opaque to Consul but is served in API responses.
|
2020-10-06 18:24:05 +00:00
|
|
|
Meta map[string]string `json:",omitempty"`
|
2018-03-28 04:33:05 +00:00
|
|
|
|
2018-06-07 04:23:27 +00:00
|
|
|
// Precedence is the order that the intention will be applied, with
|
|
|
|
// larger numbers being applied first. This is a read-only field, on
|
|
|
|
// any intention update it is updated.
|
|
|
|
Precedence int
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
// CreatedAt and UpdatedAt keep track of when this record was created
|
|
|
|
// or modified.
|
|
|
|
CreatedAt, UpdatedAt time.Time
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// Hash of the contents of the intention
|
|
|
|
//
|
|
|
|
// This is needed mainly for replication purposes. When replicating from
|
|
|
|
// one DC to another keeping the content Hash will allow us to detect
|
|
|
|
// content changes more efficiently than checking every single field
|
2020-10-06 18:24:05 +00:00
|
|
|
Hash []byte `json:",omitempty"`
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2018-05-12 02:47:26 +00:00
|
|
|
// String returns human-friendly output describing ths intention.
|
|
|
|
func (i *Intention) String() string {
|
2020-10-06 22:09:13 +00:00
|
|
|
var detail string
|
|
|
|
switch n := len(i.Permissions); n {
|
|
|
|
case 0:
|
|
|
|
detail = string(i.Action)
|
|
|
|
case 1:
|
|
|
|
detail = "1 permission"
|
|
|
|
default:
|
|
|
|
detail = fmt.Sprintf("%d permissions", len(i.Permissions))
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:42:46 +00:00
|
|
|
return fmt.Sprintf("%s => %s (%s)",
|
|
|
|
i.SourceString(),
|
|
|
|
i.DestinationString(),
|
2020-10-06 22:09:13 +00:00
|
|
|
detail)
|
2018-05-12 04:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SourceString returns the namespace/name format for the source, or
|
|
|
|
// just "name" if the namespace is the default namespace.
|
|
|
|
func (i *Intention) SourceString() string {
|
|
|
|
return i.partString(i.SourceNS, i.SourceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestinationString returns the namespace/name format for the source, or
|
|
|
|
// just "name" if the namespace is the default namespace.
|
|
|
|
func (i *Intention) DestinationString() string {
|
|
|
|
return i.partString(i.DestinationNS, i.DestinationName)
|
|
|
|
}
|
2018-05-12 02:47:26 +00:00
|
|
|
|
2018-05-12 04:42:46 +00:00
|
|
|
func (i *Intention) partString(ns, n string) string {
|
2018-05-12 05:07:58 +00:00
|
|
|
// For now we omit the default namespace from the output. In the future
|
|
|
|
// we might want to look at this and show this in a multi-namespace world.
|
|
|
|
if ns != "" && ns != IntentionDefaultNamespace {
|
2018-05-12 04:42:46 +00:00
|
|
|
n = ns + "/" + n
|
2018-05-12 02:47:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 04:42:46 +00:00
|
|
|
return n
|
2018-05-12 02:47:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 05:07:58 +00:00
|
|
|
// IntentionDefaultNamespace is the default namespace value.
|
|
|
|
const IntentionDefaultNamespace = "default"
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
// IntentionAction is the action that the intention represents. This
|
2020-05-29 18:19:16 +00:00
|
|
|
// can be "allow" or "deny" to allowlist or denylist intentions.
|
2018-03-28 04:33:05 +00:00
|
|
|
type IntentionAction string
|
|
|
|
|
|
|
|
const (
|
|
|
|
IntentionActionAllow IntentionAction = "allow"
|
|
|
|
IntentionActionDeny IntentionAction = "deny"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IntentionSourceType is the type of the source within an intention.
|
|
|
|
type IntentionSourceType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IntentionSourceConsul is a service within the Consul catalog.
|
|
|
|
IntentionSourceConsul IntentionSourceType = "consul"
|
|
|
|
)
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
// IntentionMatch are the arguments for the intention match API.
|
|
|
|
type IntentionMatch struct {
|
|
|
|
By IntentionMatchType
|
|
|
|
Names []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionMatchType is the target for a match request. For example,
|
|
|
|
// matching by source will look for all intentions that match the given
|
|
|
|
// source value.
|
|
|
|
type IntentionMatchType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
IntentionMatchSource IntentionMatchType = "source"
|
|
|
|
IntentionMatchDestination IntentionMatchType = "destination"
|
|
|
|
)
|
|
|
|
|
2018-05-12 00:19:54 +00:00
|
|
|
// IntentionCheck are the arguments for the intention check API. For
|
|
|
|
// more documentation see the IntentionCheck function.
|
|
|
|
type IntentionCheck struct {
|
|
|
|
// Source and Destination are the source and destination values to
|
|
|
|
// check. The destination is always a Consul service, but the source
|
|
|
|
// may be other values as defined by the SourceType.
|
|
|
|
Source, Destination string
|
|
|
|
|
|
|
|
// SourceType is the type of the value for the source.
|
|
|
|
SourceType IntentionSourceType
|
|
|
|
}
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
// Intentions returns the list of intentions.
|
|
|
|
func (h *Connect) Intentions(q *QueryOptions) ([]*Intention, *QueryMeta, error) {
|
|
|
|
r := h.c.newRequest("GET", "/v1/connect/intentions")
|
|
|
|
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 []*Intention
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return out, qm, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// IntentionGetExact retrieves a single intention by its unique name instead of
|
|
|
|
// its ID.
|
|
|
|
func (h *Connect) IntentionGetExact(source, destination string, q *QueryOptions) (*Intention, *QueryMeta, error) {
|
|
|
|
r := h.c.newRequest("GET", "/v1/connect/intentions/exact")
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
r.params.Set("source", source)
|
|
|
|
r.params.Set("destination", destination)
|
|
|
|
rtt, resp, err := h.c.doRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return nil, qm, nil
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Unexpected response %d: %s", resp.StatusCode, buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
var out Intention
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &out, qm, nil
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
// IntentionGet retrieves a single intention.
|
2020-10-06 18:24:05 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use IntentionGetExact instead
|
2018-03-28 17:14:32 +00:00
|
|
|
func (h *Connect) IntentionGet(id string, q *QueryOptions) (*Intention, *QueryMeta, error) {
|
|
|
|
r := h.c.newRequest("GET", "/v1/connect/intentions/"+id)
|
|
|
|
r.setQueryOptions(q)
|
2018-05-12 05:19:21 +00:00
|
|
|
rtt, resp, err := h.c.doRequest(r)
|
2018-03-28 17:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
2018-05-12 05:19:21 +00:00
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return nil, qm, nil
|
|
|
|
} else if resp.StatusCode != 200 {
|
2018-06-27 05:40:06 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"Unexpected response %d: %s", resp.StatusCode, buf.String())
|
2018-05-12 05:19:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
var out Intention
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &out, qm, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// IntentionDeleteExact deletes a single intention by its unique name instead of its ID.
|
|
|
|
func (h *Connect) IntentionDeleteExact(source, destination string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
r := h.c.newRequest("DELETE", "/v1/connect/intentions/exact")
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.params.Set("source", source)
|
|
|
|
r.params.Set("destination", destination)
|
|
|
|
|
|
|
|
rtt, resp, err := requireOK(h.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &WriteMeta{}
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2018-05-12 05:19:21 +00:00
|
|
|
// IntentionDelete deletes a single intention.
|
2020-10-06 18:24:05 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use IntentionDeleteExact instead
|
2018-05-12 05:19:21 +00:00
|
|
|
func (h *Connect) IntentionDelete(id string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
r := h.c.newRequest("DELETE", "/v1/connect/intentions/"+id)
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
rtt, resp, err := requireOK(h.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &WriteMeta{}
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
// IntentionMatch returns the list of intentions that match a given source
|
|
|
|
// or destination. The returned intentions are ordered by precedence where
|
|
|
|
// result[0] is the highest precedence (if that matches, then that rule overrides
|
|
|
|
// all other rules).
|
|
|
|
//
|
|
|
|
// Matching can be done for multiple names at the same time. The resulting
|
|
|
|
// map is keyed by the given names. Casing is preserved.
|
|
|
|
func (h *Connect) IntentionMatch(args *IntentionMatch, q *QueryOptions) (map[string][]*Intention, *QueryMeta, error) {
|
|
|
|
r := h.c.newRequest("GET", "/v1/connect/intentions/match")
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
r.params.Set("by", string(args.By))
|
|
|
|
for _, name := range args.Names {
|
|
|
|
r.params.Add("name", name)
|
|
|
|
}
|
|
|
|
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 map[string][]*Intention
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return out, qm, nil
|
|
|
|
}
|
|
|
|
|
2018-05-12 00:19:54 +00:00
|
|
|
// IntentionCheck returns whether a given source/destination would be allowed
|
|
|
|
// or not given the current set of intentions and the configuration of Consul.
|
|
|
|
func (h *Connect) IntentionCheck(args *IntentionCheck, q *QueryOptions) (bool, *QueryMeta, error) {
|
|
|
|
r := h.c.newRequest("GET", "/v1/connect/intentions/check")
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
r.params.Set("source", args.Source)
|
|
|
|
r.params.Set("destination", args.Destination)
|
|
|
|
if args.SourceType != "" {
|
|
|
|
r.params.Set("source-type", string(args.SourceType))
|
|
|
|
}
|
|
|
|
rtt, resp, err := requireOK(h.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
var out struct{ Allowed bool }
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
return out.Allowed, qm, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// IntentionUpsert will update an existing intention. The Source & Destination parameters
|
|
|
|
// in the structure must be non-empty. The ID must be empty.
|
|
|
|
func (c *Connect) IntentionUpsert(ixn *Intention, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
r := c.c.newRequest("PUT", "/v1/connect/intentions/exact")
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.params.Set("source", maybePrefixNamespace(ixn.SourceNS, ixn.SourceName))
|
|
|
|
r.params.Set("destination", maybePrefixNamespace(ixn.DestinationNS, ixn.DestinationName))
|
|
|
|
r.obj = ixn
|
|
|
|
rtt, resp, err := requireOK(c.c.doRequest(r))
|
2020-06-26 21:59:15 +00:00
|
|
|
if err != nil {
|
2020-10-06 18:24:05 +00:00
|
|
|
return nil, err
|
2020-06-26 21:59:15 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
wm := &WriteMeta{}
|
|
|
|
wm.RequestTime = rtt
|
|
|
|
return wm, nil
|
|
|
|
}
|
2020-06-26 21:59:15 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func maybePrefixNamespace(ns, name string) string {
|
|
|
|
if ns == "" {
|
|
|
|
return name
|
2020-06-26 21:59:15 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
return ns + "/" + name
|
2020-06-26 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
// IntentionCreate will create a new intention. The ID in the given
|
|
|
|
// structure must be empty and a generate ID will be returned on
|
|
|
|
// success.
|
2020-10-06 18:24:05 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use IntentionUpsert instead
|
2018-03-28 04:33:05 +00:00
|
|
|
func (c *Connect) IntentionCreate(ixn *Intention, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
r := c.c.newRequest("POST", "/v1/connect/intentions")
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.obj = ixn
|
|
|
|
rtt, resp, err := requireOK(c.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
wm := &WriteMeta{}
|
|
|
|
wm.RequestTime = rtt
|
|
|
|
|
|
|
|
var out struct{ ID string }
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return out.ID, wm, nil
|
|
|
|
}
|
2018-05-16 15:53:33 +00:00
|
|
|
|
|
|
|
// IntentionUpdate will update an existing intention. The ID in the given
|
|
|
|
// structure must be non-empty.
|
2020-10-06 18:24:05 +00:00
|
|
|
//
|
|
|
|
// Deprecated: use IntentionUpsert instead
|
2018-05-16 15:53:33 +00:00
|
|
|
func (c *Connect) IntentionUpdate(ixn *Intention, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
r := c.c.newRequest("PUT", "/v1/connect/intentions/"+ixn.ID)
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.obj = ixn
|
|
|
|
rtt, resp, err := requireOK(c.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
wm := &WriteMeta{}
|
|
|
|
wm.RequestTime = rtt
|
|
|
|
return wm, nil
|
|
|
|
}
|