Skip to content

Commit

Permalink
NVSHAS-9525: Resolve existing Go linter issues in the NeuVector repos…
Browse files Browse the repository at this point in the history
…itory (neuvector/share - part 1)
  • Loading branch information
wlin committed Oct 17, 2024
1 parent 62e0540 commit 9309ae7
Show file tree
Hide file tree
Showing 99 changed files with 908 additions and 796 deletions.
6 changes: 3 additions & 3 deletions share/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (o *CLUSUser) GetDomain(f GetAccessObjectFunc) ([]string, []string) {

domains := make([]string, len(s))
i := 0
for d, _ := range s {
for d := range s {
domains[i] = d
i++
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func (o *CLUSRegistryImageSummary) GetDomain(f GetAccessObjectFunc) ([]string, [

domains := make([]string, len(m))
i := 0
for d, _ := range m {
for d := range m {
domains[i] = d
i++
}
Expand Down Expand Up @@ -575,7 +575,7 @@ func (o *CLUSApikey) GetDomain(f GetAccessObjectFunc) ([]string, []string) {

domains := make([]string, len(s))
i := 0
for d, _ := range s {
for d := range s {
domains[i] = d
i++
}
Expand Down
1 change: 1 addition & 0 deletions share/auth/oidc/jose.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !golint
// +build !golint

// Don't lint this file. We don't want to have to add a comment to each constant.
Expand Down
10 changes: 3 additions & 7 deletions share/auth/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"time"

jose "github.com/go-jose/go-jose/v3"
"github.com/mitchellh/pointerstructure"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -82,11 +81,6 @@ type Provider struct {
remoteKeySet *remoteKeySet
}

type cachedKeys struct {
keys []jose.JSONWebKey
expiry time.Time
}

type providerJSON struct {
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
Expand Down Expand Up @@ -370,7 +364,9 @@ func (i *IDToken) Claims() (map[string]interface{}, error) {
}

var a interface{}
json.Unmarshal(i.claims, &a)
if err := json.Unmarshal(i.claims, &a); err != nil {
return nil, err
}
if m, ok := a.(map[string]interface{}); ok {
return m, nil
}
Expand Down
34 changes: 17 additions & 17 deletions share/cluster/api/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (a *ACL) Info(id string, q *QueryOptions) (*ACLEntry, *QueryMeta, error) {
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLEntry
Expand All @@ -364,7 +364,7 @@ func (a *ACL) List(q *QueryOptions) ([]*ACLEntry, *QueryMeta, error) {
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLEntry
Expand All @@ -385,7 +385,7 @@ func (a *ACL) Replication(q *QueryOptions) (*ACLReplicationStatus, *QueryMeta, e
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries *ACLReplicationStatus
Expand Down Expand Up @@ -495,7 +495,7 @@ func (a *ACL) TokenRead(tokenID string, q *QueryOptions) (*ACLToken, *QueryMeta,
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out ACLToken
Expand All @@ -519,7 +519,7 @@ func (a *ACL) TokenReadSelf(q *QueryOptions) (*ACLToken, *QueryMeta, error) {
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out ACLToken
Expand All @@ -542,7 +542,7 @@ func (a *ACL) TokenList(q *QueryOptions) ([]*ACLTokenListEntry, *QueryMeta, erro
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLTokenListEntry
Expand Down Expand Up @@ -626,7 +626,7 @@ func (a *ACL) PolicyRead(policyID string, q *QueryOptions) (*ACLPolicy, *QueryMe
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out ACLPolicy
Expand All @@ -649,7 +649,7 @@ func (a *ACL) PolicyList(q *QueryOptions) ([]*ACLPolicyListEntry, *QueryMeta, er
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLPolicyListEntry
Expand All @@ -672,7 +672,7 @@ func (a *ACL) RulesTranslate(rules io.Reader) (string, error) {
}
defer resp.Body.Close()
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

ruleBytes, err := io.ReadAll(resp.Body)
Expand All @@ -696,7 +696,7 @@ func (a *ACL) RulesTranslateToken(tokenID string) (string, error) {
}
defer resp.Body.Close()
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

ruleBytes, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -782,7 +782,7 @@ func (a *ACL) RoleRead(roleID string, q *QueryOptions) (*ACLRole, *QueryMeta, er
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if !found {
Expand All @@ -808,7 +808,7 @@ func (a *ACL) RoleReadByName(roleName string, q *QueryOptions) (*ACLRole, *Query
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if !found {
Expand Down Expand Up @@ -836,7 +836,7 @@ func (a *ACL) RoleList(q *QueryOptions) ([]*ACLRole, *QueryMeta, error) {
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLRole
Expand Down Expand Up @@ -927,7 +927,7 @@ func (a *ACL) AuthMethodRead(methodName string, q *QueryOptions) (*ACLAuthMethod
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if !found {
Expand Down Expand Up @@ -955,7 +955,7 @@ func (a *ACL) AuthMethodList(q *QueryOptions) ([]*ACLAuthMethodListEntry, *Query
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLAuthMethodListEntry
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func (a *ACL) BindingRuleRead(bindingRuleID string, q *QueryOptions) (*ACLBindin
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if !found {
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func (a *ACL) BindingRuleList(methodName string, q *QueryOptions) ([]*ACLBinding
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var entries []*ACLBindingRule
Expand Down
17 changes: 11 additions & 6 deletions share/cluster/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (a *Agent) Service(serviceID string, q *QueryOptions) (*AgentService, *Quer
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out *AgentService
Expand Down Expand Up @@ -784,7 +784,7 @@ func (a *Agent) ConnectCARoots(q *QueryOptions) (*CARootList, *QueryMeta, error)
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out CARootList
Expand All @@ -805,7 +805,7 @@ func (a *Agent) ConnectCALeaf(serviceID string, q *QueryOptions) (*LeafCert, *Qu
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out LeafCert
Expand All @@ -830,7 +830,7 @@ func (a *Agent) ConnectProxyConfig(proxyServiceID string, q *QueryOptions) (*Con
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out ConnectProxyConfig
Expand Down Expand Up @@ -1027,8 +1027,13 @@ func (a *Agent) updateTokenOnce(target, token string, q *WriteOptions) (*WriteMe

if resp.StatusCode != 200 {
var buf bytes.Buffer
io.Copy(&buf, resp.Body)
return wm, resp.StatusCode, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
var msg string
if _, err := io.Copy(&buf, resp.Body); err == nil {
msg = buf.String()
} else {
msg = "copy error: " + err.Error()
}
return wm, resp.StatusCode, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, msg)
}

return wm, resp.StatusCode, nil
Expand Down
18 changes: 12 additions & 6 deletions share/cluster/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*Quer
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if err := decodeBody(resp, out); err != nil {
Expand Down Expand Up @@ -847,6 +847,7 @@ func (c *Client) write(endpoint string, in, out interface{}, q *WriteOptions) (*
// parseQueryMeta is used to help parse query meta-data
//
// TODO(rb): bug? the error from this function is never handled
// (see https://github.com/hashicorp/consul/blob/main/api/api.go)
func parseQueryMeta(resp *http.Response, q *QueryMeta) error {
header := resp.Header

Expand Down Expand Up @@ -903,12 +904,12 @@ func parseQueryMeta(resp *http.Response, q *QueryMeta) error {
func decodeBody(resp *http.Response, out interface{}) error {
// Neuvector: improve memory usage which incurs from manipulating buffering data
data, err := io.ReadAll(resp.Body)
if err==nil && data != nil {
if err == nil && data != nil {
return json.Unmarshal(data, out)
}
return err
// dec := json.NewDecoder(resp.Body)
// return dec.Decode(out)
// dec := json.NewDecoder(resp.Body)
// return dec.Decode(out)
}

// encodeBody is used to encode a request body
Expand Down Expand Up @@ -948,9 +949,14 @@ func (req *request) filterQuery(filter string) {
// unexpected.
func generateUnexpectedResponseCodeError(resp *http.Response) error {
var buf bytes.Buffer
io.Copy(&buf, resp.Body)
var msg string
if _, err := io.Copy(&buf, resp.Body); err == nil {
msg = buf.String()
} else {
msg = "copy error: " + err.Error()
}
resp.Body.Close()
return fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
return fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, msg)
}

func requireNotFoundOrOK(d time.Duration, resp *http.Response, e error) (bool, time.Duration, *http.Response, error) {
Expand Down
8 changes: 4 additions & 4 deletions share/cluster/api/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Catalog) Nodes(q *QueryOptions) ([]*Node, *QueryMeta, error) {
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out []*Node
Expand All @@ -155,7 +155,7 @@ func (c *Catalog) Services(q *QueryOptions) (map[string][]string, *QueryMeta, er
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out map[string][]string
Expand Down Expand Up @@ -212,7 +212,7 @@ func (c *Catalog) service(service string, tags []string, q *QueryOptions, connec
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out []*CatalogService
Expand All @@ -233,7 +233,7 @@ func (c *Catalog) Node(node string, q *QueryOptions) (*CatalogNode, *QueryMeta,
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var out *CatalogNode
Expand Down
9 changes: 2 additions & 7 deletions share/cluster/api/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func (p *ProxyConfigEntry) GetModifyIndex() uint64 {
return p.ModifyIndex
}

type rawEntryListResponse struct {
kind string
Entries []map[string]interface{}
}

func makeConfigEntry(kind, name string) (ConfigEntry, error) {
switch kind {
case ServiceDefaults:
Expand Down Expand Up @@ -162,7 +157,7 @@ func (conf *ConfigEntries) Get(kind string, name string, q *QueryOptions) (Confi
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

if err := decodeBody(resp, entry); err != nil {
Expand All @@ -187,7 +182,7 @@ func (conf *ConfigEntries) List(kind string, q *QueryOptions) ([]ConfigEntry, *Q
defer resp.Body.Close()

qm := &QueryMeta{}
parseQueryMeta(resp, qm)
_ = parseQueryMeta(resp, qm)
qm.RequestTime = rtt

var raw []map[string]interface{}
Expand Down
Loading

0 comments on commit 9309ae7

Please sign in to comment.