2014-08-06 17:30:47 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2015-05-06 02:25:10 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-08-06 17:30:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeTestACL(t *testing.T, srv *HTTPServer) string {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "User Token",
|
|
|
|
"Type": "client",
|
|
|
|
"Rules": "",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/create?token=root", body)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
aclResp := obj.(aclCreateResponse)
|
|
|
|
return aclResp.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLUpdate(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
id := makeTestACL(t, srv)
|
|
|
|
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"ID": id,
|
|
|
|
"Name": "User Token 2",
|
|
|
|
"Type": "client",
|
|
|
|
"Rules": "",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/update?token=root", body)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLUpdate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
aclResp := obj.(aclCreateResponse)
|
|
|
|
if aclResp.ID != id {
|
|
|
|
t.Fatalf("bad: %v", aclResp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:25:10 +00:00
|
|
|
func TestACLUpdate_Upsert(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"ID": "my-old-id",
|
|
|
|
"Name": "User Token 2",
|
|
|
|
"Type": "client",
|
|
|
|
"Rules": "",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/update?token=root", body)
|
2015-05-06 02:25:10 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLUpdate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
aclResp := obj.(aclCreateResponse)
|
|
|
|
if aclResp.ID != "my-old-id" {
|
|
|
|
t.Fatalf("bad: %v", aclResp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:05:01 +00:00
|
|
|
func TestACLDestroy(t *testing.T) {
|
2014-08-06 17:30:47 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
id := makeTestACL(t, srv)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/destroy/"+id+"?token=root", nil)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2014-08-18 19:05:01 +00:00
|
|
|
obj, err := srv.ACLDestroy(resp, req)
|
2014-08-06 17:30:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
if resp, ok := obj.(bool); !ok || !resp {
|
2014-08-06 17:30:47 +00:00
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/acl/info/"+id, nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.ACLGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.ACLs)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 0 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
2014-08-06 17:30:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLClone(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
id := makeTestACL(t, srv)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/acl/clone/"+id+"?token=root", nil)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLClone(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
aclResp, ok := obj.(aclCreateResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work: %#v %#v", obj, resp)
|
|
|
|
}
|
|
|
|
if aclResp.ID == id {
|
|
|
|
t.Fatalf("bad id")
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/acl/info/"+aclResp.ID, nil)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.ACLGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.ACLs)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLGet(t *testing.T) {
|
2015-11-15 05:05:37 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/acl/info/nope", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.ACLs)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if respObj == nil || len(respObj) != 0 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-08-06 17:30:47 +00:00
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
id := makeTestACL(t, srv)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/acl/info/"+id, nil)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.ACLs)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACLList(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
|
|
|
var ids []string
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ids = append(ids, makeTestACL(t, srv))
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/acl/list?token=root", nil)
|
2014-08-06 17:30:47 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLList(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.ACLs)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2014-08-12 21:48:36 +00:00
|
|
|
|
2014-08-19 21:28:49 +00:00
|
|
|
// 10 + anonymous + master
|
|
|
|
if len(respObj) != 12 {
|
2014-08-06 17:30:47 +00:00
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-08-05 04:32:36 +00:00
|
|
|
|
|
|
|
func TestACLReplicationStatus(t *testing.T) {
|
|
|
|
httpTest(t, func(srv *HTTPServer) {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/acl/replication", nil)
|
2016-08-05 04:32:36 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.ACLReplicationStatus(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
_, ok := obj.(structs.ACLReplicationStatus)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|