Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
priitp committed Aug 10, 2024
1 parent ee1c314 commit 1034c79
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 65 deletions.
4 changes: 2 additions & 2 deletions plugin/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func callBackend(path string, op logical.Operation, vars ...interface{}) (*logic
case *logical.Secret:
s = v.(*logical.Secret)
default:
return nil, errors.New(fmt.Sprintf("Wrong type of argument: %s", t))
return nil, fmt.Errorf("Wrong type of argument: %s", t)
}
}
} else if len(vars) > 3 {
return nil, errors.New(fmt.Sprintf("Wrong number of arguments: %d", len(vars)))
return nil, fmt.Errorf("Wrong number of arguments: %d", len(vars))
}
if b == nil || cfg == nil {
b, cfg = getBackend()
Expand Down
2 changes: 1 addition & 1 deletion plugin/config_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (b *backend) pathConfigList() *framework.Path {
}
}

func (b *backend) listConfigs(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
func (b *backend) listConfigs(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
logger := b.Backend.Logger()
b.bLock.RLock()
defer b.bLock.RUnlock()
Expand Down
2 changes: 1 addition & 1 deletion plugin/config_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func TestConfigRead(t *testing.T) {
t.Fatal(resp.Error())
}
if resp != nil {
t.Fatal("Got response, expected nil!")
t.Log(resp)
t.Fatal("Got response, expected nil!")
}
}

Expand Down
13 changes: 6 additions & 7 deletions plugin/creds_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (

const (
SecretType = "password"
MaxTTL = time.Duration(11000000000000000)
)

var MaxTTL = time.Duration(11000000000000000)

func conf_data(configName string) *framework.FieldData {
func confData(configName string) *framework.FieldData {
return &framework.FieldData{
Raw: map[string]interface{}{
"config_name": configName,
Expand Down Expand Up @@ -98,7 +97,7 @@ func (b *backend) rotateCreds(ctx context.Context, req *logical.Request, data *f
}
logger.Debug("rotateCreds", "role", role)

cfg, err := b.fetchConfig(ctx, req, conf_data(role.ConfigName))
cfg, err := b.fetchConfig(ctx, req, confData(role.ConfigName))
if err != nil {
logger.Error("rotateCreds", "error", err)
return nil, err
Expand Down Expand Up @@ -134,7 +133,7 @@ func (b *backend) rotateCreds(ctx context.Context, req *logical.Request, data *f
"role": role.Name,
"acl_profile": role.ACLProfile,
"client_profile": role.ClientProfile,
"ttl": role.Ttl,
"ttl": role.TTL,
//"guaranteed_endpoint_permission_override": role.GuaranteedEndpointPermissionOverride,
"guaranteed_endpoint_permission_override": true,
"subscription_manager": role.SubscriptionManager,
Expand All @@ -148,7 +147,7 @@ func (b *backend) rotateCreds(ctx context.Context, req *logical.Request, data *f
resp := b.Secret(SecretType).Response(secretD, internalD)
resp.Secret.MaxTTL = MaxTTL

resp.Secret.TTL = role.Ttl * time.Second
resp.Secret.TTL = role.TTL * time.Second
return resp, nil

}
Expand Down Expand Up @@ -177,7 +176,7 @@ func (b *backend) revokeCreds(ctx context.Context, req *logical.Request, data *f
return logical.ErrorResponse("role not found", "role", roleRaw.(string)), nil
}

cfg, err := b.fetchConfig(ctx, req, conf_data(role.ConfigName))
cfg, err := b.fetchConfig(ctx, req, confData(role.ConfigName))
if err != nil {
logger.Error("rotateCreds", "error", err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions plugin/creds_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRotateCreds(t *testing.T) {
}
gepo := resp.Data["guaranteed_endpoint_permission_override"].(bool)
if gepo != guaranteedEndpointPermissionOverride {
t.Fatal(fmt.Sprintf("Expected GuaranteedEndpointPermissionOverride: %t, received %t", guaranteedEndpointPermissionOverride, gepo))
t.Fatalf("Expected GuaranteedEndpointPermissionOverride: %t, received %t", guaranteedEndpointPermissionOverride, gepo)
}

if !strings.HasPrefix(user, testUserPrefix+"-") {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestPrefixCreds(t *testing.T) {
"username": user,
"role": testRoleName,
}
resp, err = callBackend("creds/", logical.RevokeOperation, pl, b, cfg, resp.Secret)
_, err = callBackend("creds/", logical.RevokeOperation, pl, b, cfg, resp.Secret)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Role struct {
Name string
Vpn string
Ttl time.Duration
TTL time.Duration
ConfigName string
ACLProfile string
ClientProfile string
Expand All @@ -20,7 +20,7 @@ type Role struct {
}

func (r *Role) String() string {
return fmt.Sprintf("name=%s, vpn=%s, ttl=%s, acl=%s, client_profile=%s, config_name=%s, guaranteed_endpoint_permission_override=%t, subscription_manager=%t, username_prefix=%s", r.Name, r.Vpn, r.Ttl.String(), r.ACLProfile, r.ClientProfile, r.ConfigName, r.GuaranteedEndpointPermissionOverride, r.SubscriptionManager, r.UsernamePrefix)
return fmt.Sprintf("name=%s, vpn=%s, ttl=%s, acl=%s, client_profile=%s, config_name=%s, guaranteed_endpoint_permission_override=%t, subscription_manager=%t, username_prefix=%s", r.Name, r.Vpn, r.TTL.String(), r.ACLProfile, r.ClientProfile, r.ConfigName, r.GuaranteedEndpointPermissionOverride, r.SubscriptionManager, r.UsernamePrefix)
}

func data2role(data *framework.FieldData) (*Role, error) {
Expand All @@ -42,7 +42,7 @@ func data2role(data *framework.FieldData) (*Role, error) {

ttlRaw, ok := data.GetOk("ttl")
if ok {
role.Ttl = time.Duration(ttlRaw.(int))
role.TTL = time.Duration(ttlRaw.(int))
}

aclRaw, ok := data.GetOk("acl_profile")
Expand Down
44 changes: 22 additions & 22 deletions plugin/role_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (b *backend) pathRoleList() *framework.Path {
}
}

func (b *backend) listRoles(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
func (b *backend) listRoles(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
logger := b.Backend.Logger()
b.bLock.RLock()
defer b.bLock.RUnlock()
Expand Down Expand Up @@ -115,7 +115,7 @@ func (b *backend) fetchRole(ctx context.Context, req *logical.Request, name stri
type Role1 struct {
Name string
Vpn string
Ttl string
TTL string
ConfigName string
ACLProfile string
ClientProfile string
Expand All @@ -125,14 +125,14 @@ func (b *backend) fetchRole(ctx context.Context, req *logical.Request, name stri
if err != nil {
return nil, err
}
ttl, err := time.ParseDuration(dr.Ttl + "s")
ttl, err := time.ParseDuration(dr.TTL + "s")
if err != nil {
return nil, err
}
return &Role{
Name: dr.Name,
Vpn: dr.Vpn,
Ttl: ttl,
TTL: ttl,
ConfigName: dr.ConfigName,
ACLProfile: dr.ACLProfile,
ClientProfile: dr.ClientProfile,
Expand Down Expand Up @@ -164,7 +164,7 @@ func (b *backend) readRole(ctx context.Context, req *logical.Request, data *fram
Data: map[string]interface{}{
"name": dummy.Name,
"vpn": dummy.Vpn,
"ttl": dummy.Ttl,
"ttl": dummy.TTL,
"acl_profile": dummy.ACLProfile,
"client_profile": dummy.ClientProfile,
"config_name": dummy.ConfigName,
Expand All @@ -190,7 +190,7 @@ func (b *backend) createRole(ctx context.Context, req *logical.Request, data *fr
if len(role.Vpn) == 0 {
return logical.ErrorResponse("createRole", "vpn is required"), nil
}
if role.Ttl == 0 {
if role.TTL == 0 {
return logical.ErrorResponse("ttl is required"), nil
}
if len(role.ConfigName) == 0 {
Expand Down Expand Up @@ -219,7 +219,7 @@ func (b *backend) createRole(ctx context.Context, req *logical.Request, data *fr
Data: map[string]interface{}{
"name": role.Name,
"Vpn": role.Vpn,
"Ttl": time.Duration(role.Ttl * time.Second).String(),
"ttl": time.Duration(role.TTL * time.Second).String(),
"acl_profile": role.ACLProfile,
"client_profile": role.ClientProfile,
"config_name": role.ConfigName,
Expand Down Expand Up @@ -255,34 +255,34 @@ func (b *backend) updateRole(ctx context.Context, req *logical.Request, data *fr
}
logger.Debug("updateRole", "role from storage", role)

role_to_update, err := data2role(data)
roleToUpdate, err := data2role(data)
if err != nil {
return logical.ErrorResponse("updateRole", "error", "Missing role name in request"), nil
}
logger.Debug("updateRole", "role to update", role_to_update)
logger.Debug("updateRole", "role to update", roleToUpdate)

if role.Vpn != role_to_update.Vpn && len(role_to_update.Vpn) > 0 {
role.Vpn = role_to_update.Vpn
if role.Vpn != roleToUpdate.Vpn && len(roleToUpdate.Vpn) > 0 {
role.Vpn = roleToUpdate.Vpn
}

if role.Ttl != role_to_update.Ttl {
role.Ttl = role_to_update.Ttl
if role.TTL != roleToUpdate.TTL {
role.TTL = roleToUpdate.TTL
}

if role.ConfigName != role_to_update.ConfigName && len(role_to_update.ConfigName) > 0 {
role.ConfigName = role_to_update.ConfigName
if role.ConfigName != roleToUpdate.ConfigName && len(roleToUpdate.ConfigName) > 0 {
role.ConfigName = roleToUpdate.ConfigName
}

if role.ACLProfile != role_to_update.ACLProfile && len(role_to_update.ACLProfile) > 0 {
role.ACLProfile = role_to_update.ACLProfile
if role.ACLProfile != roleToUpdate.ACLProfile && len(roleToUpdate.ACLProfile) > 0 {
role.ACLProfile = roleToUpdate.ACLProfile
}

if role.ClientProfile != role_to_update.ClientProfile && len(role_to_update.ClientProfile) > 0 {
role.ClientProfile = role_to_update.ClientProfile
if role.ClientProfile != roleToUpdate.ClientProfile && len(roleToUpdate.ClientProfile) > 0 {
role.ClientProfile = roleToUpdate.ClientProfile
}

if role.UsernamePrefix != role_to_update.UsernamePrefix && len(role_to_update.UsernamePrefix) > 0 {
role.UsernamePrefix = role_to_update.UsernamePrefix
if role.UsernamePrefix != roleToUpdate.UsernamePrefix && len(roleToUpdate.UsernamePrefix) > 0 {
role.UsernamePrefix = roleToUpdate.UsernamePrefix
}

// No way to tell if 'false' came from the data or is just uncheck, so override data2role() here.
Expand Down Expand Up @@ -314,7 +314,7 @@ func (b *backend) updateRole(ctx context.Context, req *logical.Request, data *fr
Data: map[string]interface{}{
"name": role.Name,
"Vpn": role.Vpn,
"Ttl": time.Duration(role.Ttl * time.Second).String(),
"ttl": time.Duration(role.TTL * time.Second).String(),
"acl_profile": role.ACLProfile,
"client_profile": role.ClientProfile,
"config_name": role.ConfigName,
Expand Down
28 changes: 17 additions & 11 deletions plugin/role_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package solace

import (
"context"
"fmt"
logical "github.com/hashicorp/vault/sdk/logical"
"testing"
"time"
Expand All @@ -13,7 +12,7 @@ const (
testRolePath = "roles/test1role"
wrongRole = "does-not-exist"
wrongRolePath = "roles/does-not-exist"
credTtl = 1
credTTL = 1
guaranteedEndpointPermissionOverride = true
testUserPrefix = "slowBoring"
)
Expand Down Expand Up @@ -55,7 +54,7 @@ func createRole(b logical.Backend, cfg *logical.BackendConfig) (*logical.Respons
pl := map[string]interface{}{
"name": testRoleName,
"vpn": testVpn,
"ttl": credTtl,
"ttl": credTTL,
"acl_profile": aclProfile,
"client_profile": nil,
"config_name": "default",
Expand Down Expand Up @@ -113,6 +112,9 @@ func TestCreateRole(t *testing.T) {
if resp != nil {
t.Fatal("Found deleted role")
}
if err != nil {
t.Fatal(err)
}
}

func TestReadRole(t *testing.T) {
Expand Down Expand Up @@ -143,14 +145,17 @@ func TestDeleteRole(t *testing.T) {

func TestUpdateRole(t *testing.T) {
b, cfg := getBackend(t)
resp, err := createRole(b, cfg)
_, err := createRole(b, cfg)
if err != nil {
t.Fatal(err)
}
pl := map[string]interface{}{
"name": testRoleName,
"ttl": "0s",
"acl_profile": aclProfile,
"client_profile": clientProfile,
}
resp, err = b.HandleRequest(context.Background(), &logical.Request{
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: testRolePath,
Data: pl,
Expand All @@ -174,28 +179,29 @@ func TestUpdateRole(t *testing.T) {
t.Log(resp.Data)
role := Role{
Vpn: resp.Data["vpn"].(string),
Ttl: time.Duration(resp.Data["ttl"].(time.Duration)),
TTL: time.Duration(resp.Data["ttl"].(time.Duration)),
ACLProfile: resp.Data["acl_profile"].(string),
ClientProfile: resp.Data["client_profile"].(string),
GuaranteedEndpointPermissionOverride: resp.Data["guaranteed_endpoint_permission_override"].(bool),
UsernamePrefix: resp.Data["username_prefix"].(string),
}
if role.Ttl.String() != pl["ttl"] {
t.Fatal("TTLs are different, ttl set = " + pl["ttl"].(string) + ", ttl received = " + role.Ttl.String())
if role.TTL.String() != pl["ttl"] {
t.Fatal("TTLs are different, ttl set = " + pl["ttl"].(string) + ", ttl received = " + role.TTL.String())
}

if role.Vpn != testVpn {
t.Fatal("Vpn disappeared")
}
if role.ACLProfile != aclProfile {
t.Fatal("ACL profile disappeared, profile read = " + resp.Data["acl_profile"].(string))
t.Fatalf("ACL profile disappeared, profile read = %s", resp.Data["acl_profile"].(string))
}
if role.ClientProfile != clientProfile {
t.Fatal("Client profile disappeared")
}
if role.GuaranteedEndpointPermissionOverride != guaranteedEndpointPermissionOverride {
t.Fatal(fmt.Sprintf("GuaranteedEndpointPermissionOverride received: %t, needed: %t", role.GuaranteedEndpointPermissionOverride, guaranteedEndpointPermissionOverride))
t.Fatalf("GuaranteedEndpointPermissionOverride received: %t, needed: %t", role.GuaranteedEndpointPermissionOverride, guaranteedEndpointPermissionOverride)
}
if role.UsernamePrefix != testUserPrefix {
t.Fatal(fmt.Sprintf("Received username_prefix: %s, expected: %s", role.UsernamePrefix, testUserPrefix))
t.Fatalf("Received username_prefix: %s, expected: %s", role.UsernamePrefix, testUserPrefix)
}
}
4 changes: 2 additions & 2 deletions plugin/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestRole(t *testing.T) {
}

input.Raw["name"] = nil
rl, err = data2role(input)
_, err = data2role(input)
if err == nil {
t.Fatal("Zero-length role name is ok!")
}

delete(input.Raw, "name")
rl, err = data2role(input)
_, err = data2role(input)
if err == nil {
t.Fatal("Missing role name is ok!")
}
Expand Down
7 changes: 3 additions & 4 deletions plugin/semp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/go-openapi/strfmt"
hclog "github.com/hashicorp/go-hclog"
"io"
"io/ioutil"
all "kindredgroup.com/solace-plugin/gen/solaceapi/all"
"net/http"
"net/url"
Expand All @@ -17,7 +16,7 @@ import (
// getClient returns SEMP v2 client
func getClient(cfg *solaceConfig, logger hclog.Logger) (all.ClientService, error) {
accessSchemes := []string{"http", "https"}
if cfg.DisableTls {
if cfg.DisableTLS {
accessSchemes = []string{"http"}
}
hosts := strings.Split(cfg.SolaceHost, ",")
Expand Down Expand Up @@ -58,7 +57,7 @@ func getPrimary(hosts []string, cfg *solaceConfig, logger hclog.Logger) string {
func isActive(host string, cfg *solaceConfig, logger hclog.Logger) bool {
var scheme string
logger.Debug("Host: " + host)
if cfg.DisableTls {
if cfg.DisableTLS {
scheme = "http"
} else {
scheme = "https"
Expand Down Expand Up @@ -87,7 +86,7 @@ func isActive(host string, cfg *solaceConfig, logger hclog.Logger) bool {
logger.Info("isActive", "Got response code", resp.Status)
return false
}
out, err := ioutil.ReadAll(resp.Body)
out, err := io.ReadAll(resp.Body)
if err != nil {
logger.Error("isActive", "error while reading response body", err.Error())
return false
Expand Down
Loading

0 comments on commit 1034c79

Please sign in to comment.