* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
376 lines
9.2 KiB
Go
376 lines
9.2 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package loopback
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
|
|
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostsets"
|
|
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
|
ta "github.com/stretchr/testify/assert"
|
|
tr "github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
// TestLoopbackHostPlugin is a quick test of basic host service functionality.
|
|
func TestLoopbackHostPlugin(t *testing.T) {
|
|
require, assert := tr.New(t), ta.New(t)
|
|
ctx := context.Background()
|
|
|
|
plg, err := NewLoopbackPlugin()
|
|
require.NoError(err)
|
|
secretsMap := map[string]any{
|
|
"key1": "key2",
|
|
"baz": true,
|
|
}
|
|
secrets, err := structpb.NewStruct(secretsMap)
|
|
require.NoError(err)
|
|
|
|
// First, test that if we give it secrets, those secrets come back as
|
|
// persisted data
|
|
catResp, err := plg.OnCreateCatalog(ctx, &plgpb.OnCreateCatalogRequest{
|
|
Catalog: &hostcatalogs.HostCatalog{
|
|
Secrets: secrets,
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
require.NotNil(catResp)
|
|
require.NotNil(catResp.GetPersisted())
|
|
require.NotNil(catResp.GetPersisted().GetSecrets())
|
|
assert.EqualValues(secretsMap, catResp.GetPersisted().GetSecrets().AsMap())
|
|
|
|
newSecretsMap := map[string]any{
|
|
"key1": "key2",
|
|
"baz": true,
|
|
}
|
|
newSecrets, err := structpb.NewStruct(newSecretsMap)
|
|
require.NoError(err)
|
|
|
|
// First, test that if we give it secrets, those secrets come back as
|
|
// persisted data
|
|
upResp, err := plg.OnUpdateCatalog(ctx, &plgpb.OnUpdateCatalogRequest{
|
|
CurrentCatalog: &hostcatalogs.HostCatalog{},
|
|
NewCatalog: &hostcatalogs.HostCatalog{
|
|
Secrets: newSecrets,
|
|
},
|
|
Persisted: &plgpb.HostCatalogPersisted{
|
|
Secrets: secrets,
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
require.NotNil(upResp)
|
|
require.NotNil(upResp.GetPersisted())
|
|
require.NotNil(upResp.GetPersisted().GetSecrets())
|
|
assert.EqualValues(newSecretsMap, upResp.GetPersisted().GetSecrets().AsMap())
|
|
|
|
// Add data to some sets
|
|
hostInfo1 := map[string]any{
|
|
loopbackPluginHostInfoAttrField: map[string]any{
|
|
"set_ids": []any{"set1"},
|
|
"external_id": "host1",
|
|
"ip_addresses": []any{"1.2.3.4", "2.3.4.5"},
|
|
"dns_names": []any{"foo.com"},
|
|
},
|
|
}
|
|
attrs, err := structpb.NewStruct(hostInfo1)
|
|
require.NoError(err)
|
|
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
|
Set: &hostsets.HostSet{
|
|
Id: "set1",
|
|
Attrs: &hostsets.HostSet_Attributes{
|
|
Attributes: attrs,
|
|
},
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
hostInfo2 := map[string]any{
|
|
loopbackPluginHostInfoAttrField: map[string]any{
|
|
"set_ids": []any{"set2"},
|
|
"external_id": "host2",
|
|
"ip_addresses": []any{"5.6.7.8", "6.7.8.9"},
|
|
"dns_names": []any{"bar.com"},
|
|
},
|
|
}
|
|
attrs, err = structpb.NewStruct(hostInfo2)
|
|
require.NoError(err)
|
|
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
|
Set: &hostsets.HostSet{
|
|
Id: "set2",
|
|
Attrs: &hostsets.HostSet_Attributes{
|
|
Attributes: attrs,
|
|
},
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
|
|
// Define test struct and validation function
|
|
type testInfo struct {
|
|
name string
|
|
sets []string
|
|
found []map[string]any
|
|
}
|
|
validateSets := func(t *testing.T, tt testInfo) {
|
|
require, assert := tr.New(t), ta.New(t)
|
|
var hostSets []*hostsets.HostSet
|
|
for _, set := range tt.sets {
|
|
hostSets = append(hostSets, &hostsets.HostSet{Id: set})
|
|
}
|
|
resp, err := plg.ListHosts(ctx, &plgpb.ListHostsRequest{
|
|
Sets: hostSets,
|
|
})
|
|
require.NoError(err)
|
|
if len(tt.found) == 0 {
|
|
assert.Len(resp.GetHosts(), 0)
|
|
return
|
|
}
|
|
|
|
require.Greater(len(resp.GetHosts()), 0)
|
|
|
|
var found []map[string]any
|
|
for _, host := range resp.GetHosts() {
|
|
hostMap := map[string]any{
|
|
"external_id": host.GetExternalId(),
|
|
}
|
|
var sets []any
|
|
for _, set := range host.SetIds {
|
|
sets = append(sets, set)
|
|
}
|
|
var ips []any
|
|
for _, ip := range host.GetIpAddresses() {
|
|
ips = append(ips, ip)
|
|
}
|
|
var names []any
|
|
for _, name := range host.GetDnsNames() {
|
|
names = append(names, name)
|
|
}
|
|
if len(sets) > 0 {
|
|
hostMap["set_ids"] = sets
|
|
}
|
|
if len(ips) > 0 {
|
|
hostMap["ip_addresses"] = ips
|
|
}
|
|
if len(names) > 0 {
|
|
hostMap["dns_names"] = names
|
|
}
|
|
found = append(found, hostMap)
|
|
}
|
|
assert.ElementsMatch(tt.found, found)
|
|
}
|
|
|
|
// First set of tests: check that we can look up sets individually and
|
|
// together
|
|
setTests := []testInfo{
|
|
{
|
|
name: "set 1",
|
|
sets: []string{"set1"},
|
|
found: []map[string]any{
|
|
hostInfo1[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
},
|
|
},
|
|
{
|
|
name: "set 2",
|
|
sets: []string{"set2"},
|
|
found: []map[string]any{
|
|
hostInfo2[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
},
|
|
},
|
|
{
|
|
name: "sets 1 and 2",
|
|
sets: []string{"set1", "set2"},
|
|
found: []map[string]any{
|
|
hostInfo1[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
hostInfo2[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range setTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
validateSets(t, tt)
|
|
})
|
|
}
|
|
|
|
// Remove a set
|
|
_, err = plg.OnDeleteSet(ctx, &plgpb.OnDeleteSetRequest{
|
|
Set: &hostsets.HostSet{
|
|
Id: "set1",
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
|
|
// Run tests again, making sure we no longer find that host either
|
|
// individually or together
|
|
setTests = []testInfo{
|
|
{
|
|
name: "set 1 deleted",
|
|
sets: []string{"set1"},
|
|
found: []map[string]any{},
|
|
},
|
|
{
|
|
name: "set 2 not deleted",
|
|
sets: []string{"set2"},
|
|
found: []map[string]any{
|
|
hostInfo2[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
},
|
|
},
|
|
{
|
|
name: "sets 1 and 2 set 1 deleted",
|
|
sets: []string{"set1", "set2"},
|
|
found: []map[string]any{
|
|
hostInfo2[loopbackPluginHostInfoAttrField].(map[string]any),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range setTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
validateSets(t, tt)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoopbackHostPluginArrays(t *testing.T) {
|
|
require := tr.New(t)
|
|
ctx := context.Background()
|
|
|
|
plg, err := NewLoopbackPlugin()
|
|
require.NoError(err)
|
|
|
|
// Add data to some sets
|
|
hostInfo1 := map[string]any{
|
|
loopbackPluginHostInfoAttrField: []any{
|
|
map[string]any{
|
|
"set_ids": []any{"set1"},
|
|
"external_id": "host1a",
|
|
"ip_addresses": []any{"1.2.3.4", "2.3.4.5"},
|
|
"dns_names": []any{"foo.com"},
|
|
},
|
|
map[string]any{
|
|
"set_ids": []any{"set1"},
|
|
"external_id": "host1b",
|
|
"ip_addresses": []any{"3.4.5.6", "4.5.6.7"},
|
|
"dns_names": []any{"bar.com"},
|
|
},
|
|
},
|
|
}
|
|
attrs, err := structpb.NewStruct(hostInfo1)
|
|
require.NoError(err)
|
|
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
|
Set: &hostsets.HostSet{
|
|
Id: "set1",
|
|
Attrs: &hostsets.HostSet_Attributes{
|
|
Attributes: attrs,
|
|
},
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
hostInfo2 := map[string]any{
|
|
loopbackPluginHostInfoAttrField: []any{
|
|
map[string]any{
|
|
"set_ids": []any{"set2"},
|
|
"external_id": "host2a",
|
|
"ip_addresses": []any{"10.20.30.40", "20.30.40.50"},
|
|
"dns_names": []any{"foz.com"},
|
|
},
|
|
map[string]any{
|
|
"set_ids": []any{"set2"},
|
|
"external_id": "host2b",
|
|
"ip_addresses": []any{"30.40.50.60", "40.50.60.70"},
|
|
"dns_names": []any{"baz.com"},
|
|
},
|
|
},
|
|
}
|
|
attrs, err = structpb.NewStruct(hostInfo2)
|
|
require.NoError(err)
|
|
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
|
Set: &hostsets.HostSet{
|
|
Id: "set2",
|
|
Attrs: &hostsets.HostSet_Attributes{
|
|
Attributes: attrs,
|
|
},
|
|
},
|
|
})
|
|
require.NoError(err)
|
|
|
|
// Define test struct and validation function
|
|
type testInfo struct {
|
|
name string
|
|
sets []string
|
|
found []any
|
|
}
|
|
validateSets := func(t *testing.T, tt testInfo) {
|
|
require, assert := tr.New(t), ta.New(t)
|
|
var hostSets []*hostsets.HostSet
|
|
for _, set := range tt.sets {
|
|
hostSets = append(hostSets, &hostsets.HostSet{Id: set})
|
|
}
|
|
resp, err := plg.ListHosts(ctx, &plgpb.ListHostsRequest{
|
|
Sets: hostSets,
|
|
})
|
|
require.NoError(err)
|
|
if len(tt.found) == 0 {
|
|
assert.Len(resp.GetHosts(), 0)
|
|
return
|
|
}
|
|
|
|
require.Greater(len(resp.GetHosts()), 0)
|
|
|
|
var found []any
|
|
for _, host := range resp.GetHosts() {
|
|
hostMap := map[string]any{
|
|
"external_id": host.GetExternalId(),
|
|
}
|
|
var sets []any
|
|
for _, set := range host.SetIds {
|
|
sets = append(sets, set)
|
|
}
|
|
var ips []any
|
|
for _, ip := range host.GetIpAddresses() {
|
|
ips = append(ips, ip)
|
|
}
|
|
var names []any
|
|
for _, name := range host.GetDnsNames() {
|
|
names = append(names, name)
|
|
}
|
|
if len(sets) > 0 {
|
|
hostMap["set_ids"] = sets
|
|
}
|
|
if len(ips) > 0 {
|
|
hostMap["ip_addresses"] = ips
|
|
}
|
|
if len(names) > 0 {
|
|
hostMap["dns_names"] = names
|
|
}
|
|
found = append(found, hostMap)
|
|
}
|
|
assert.ElementsMatch(tt.found, found)
|
|
}
|
|
|
|
// First set of tests: check that we can look up sets individually and
|
|
// together
|
|
setTests := []testInfo{
|
|
{
|
|
name: "set 1",
|
|
sets: []string{"set1"},
|
|
found: hostInfo1[loopbackPluginHostInfoAttrField].([]any),
|
|
},
|
|
{
|
|
name: "set 2",
|
|
sets: []string{"set2"},
|
|
found: hostInfo2[loopbackPluginHostInfoAttrField].([]any),
|
|
},
|
|
{
|
|
name: "sets 1 and 2",
|
|
sets: []string{"set1", "set2"},
|
|
found: append(hostInfo1[loopbackPluginHostInfoAttrField].([]any),
|
|
hostInfo2[loopbackPluginHostInfoAttrField].([]any)...),
|
|
},
|
|
}
|
|
for _, tt := range setTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
validateSets(t, tt)
|
|
})
|
|
}
|
|
}
|