Skip to content

Commit

Permalink
Merge pull request #556 from nokia/hide-passwords-api
Browse files Browse the repository at this point in the history
Do not return targets passwords in REST API requests
  • Loading branch information
karimra authored Nov 25, 2024
2 parents 235990e + 17f99c7 commit 13a5f9e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
83 changes: 83 additions & 0 deletions pkg/api/types/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,89 @@ func (tc TargetConfig) String() string {
return string(b)
}

func (tc *TargetConfig) DeepCopy() *TargetConfig {
if tc == nil {
return nil
}
ntc := &TargetConfig{
Name: tc.Name,
Address: tc.Address,
AuthScheme: tc.AuthScheme,
Timeout: tc.Timeout,
TLSServerName: tc.TLSServerName,
Subscriptions: make([]string, 0, len(tc.Subscriptions)),
Outputs: make([]string, 0, len(tc.Outputs)),
BufferSize: tc.BufferSize,
RetryTimer: tc.RetryTimer,
TLSMinVersion: tc.TLSMinVersion,
TLSMaxVersion: tc.TLSMaxVersion,
TLSVersion: tc.TLSVersion,
ProtoFiles: make([]string, 0, len(tc.ProtoFiles)),
ProtoDirs: make([]string, 0, len(tc.ProtoDirs)),
Tags: make([]string, 0, len(tc.Tags)),
EventTags: make(map[string]string, len(tc.EventTags)),
Proxy: tc.Proxy,
TunnelTargetType: tc.TunnelTargetType,
Metadata: make(map[string]string, len(tc.Metadata)),
CipherSuites: make([]string, 0, len(tc.CipherSuites)),
TCPKeepalive: tc.TCPKeepalive,
}
if tc.Username != nil {
ntc.Username = tc.Username
}
if tc.Password != nil {
ntc.Password = tc.Password
}
if tc.Insecure != nil {
ntc.Insecure = tc.Insecure
}
if tc.TLSCA != nil {
ntc.TLSCA = tc.TLSCA
}
if tc.TLSCert != nil {
ntc.TLSCert = tc.TLSCert
}
if tc.TLSKey != nil {
ntc.TLSKey = tc.TLSKey
}
if tc.SkipVerify != nil {
ntc.SkipVerify = tc.SkipVerify
}
if tc.LogTLSSecret != nil {
ntc.LogTLSSecret = tc.LogTLSSecret
}
if tc.Gzip != nil {
ntc.Gzip = tc.Gzip
}
if tc.Token != nil {
ntc.Token = tc.Token
}
if tc.Encoding != nil {
ntc.Encoding = tc.Encoding
}
ntc.Subscriptions = append(ntc.Subscriptions, tc.Subscriptions...)
ntc.Outputs = append(ntc.Outputs, tc.Outputs...)
ntc.ProtoFiles = append(ntc.ProtoFiles, tc.ProtoFiles...)
ntc.ProtoDirs = append(ntc.ProtoDirs, tc.ProtoDirs...)
ntc.Tags = append(ntc.Tags, tc.Tags...)
ntc.CipherSuites = append(ntc.CipherSuites, tc.CipherSuites...)

for k, v := range tc.EventTags {
tc.EventTags[k] = v
}
for k, v := range tc.Metadata {
tc.Metadata[k] = v
}
if tc.GRPCKeepalive != nil {
ntc.GRPCKeepalive = &clientKeepalive{
Time: tc.GRPCKeepalive.Time,
Timeout: tc.GRPCKeepalive.Timeout,
PermitWithoutStream: tc.GRPCKeepalive.PermitWithoutStream,
}
}
return ntc
}

func (tc *TargetConfig) SetTLSConfig(tlsConfig *tls.Config) {
tc.tlsConfig = tlsConfig
}
Expand Down
38 changes: 35 additions & 3 deletions pkg/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
"net/http"
"strings"

"github.com/AlekSi/pointer"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/openconfig/gnmic/pkg/api/types"
"github.com/openconfig/gnmic/pkg/api/utils"
"github.com/openconfig/gnmic/pkg/config"
)

func (a *App) newAPIServer() (*http.Server, error) {
Expand Down Expand Up @@ -78,15 +80,24 @@ func (a *App) handleConfigTargetsGet(w http.ResponseWriter, r *http.Request) {
a.configLock.RLock()
defer a.configLock.RUnlock()
if id == "" {
err = json.NewEncoder(w).Encode(a.Config.Targets)
// copy targets map
targets := make(map[string]*types.TargetConfig, len(a.Config.Targets))
for n, tc := range a.Config.Targets {
ntc := tc.DeepCopy()
ntc.Password = pointer.ToString("****")
targets[n] = ntc
}
err = json.NewEncoder(w).Encode(targets)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}})
}
return
}
if t, ok := a.Config.Targets[id]; ok {
err = json.NewEncoder(w).Encode(t)
tc := t.DeepCopy()
tc.Password = pointer.ToString("****")
err = json.NewEncoder(w).Encode(tc)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}})
Expand Down Expand Up @@ -192,7 +203,28 @@ func (a *App) handleConfigProcessors(w http.ResponseWriter, r *http.Request) {
}

func (a *App) handleConfig(w http.ResponseWriter, r *http.Request) {
a.handlerCommonGet(w, a.Config)
nc := &config.Config{
GlobalFlags: a.Config.GlobalFlags,
LocalFlags: a.Config.LocalFlags,
FileConfig: a.Config.FileConfig,
Targets: make(map[string]*types.TargetConfig, len(a.Config.Targets)),
Subscriptions: a.Config.Subscriptions,
Outputs: a.Config.Outputs,
Inputs: a.Config.Inputs,
Processors: a.Config.Processors,
Clustering: a.Config.Clustering,
GnmiServer: a.Config.GnmiServer,
APIServer: a.Config.APIServer,
Loader: a.Config.Loader,
Actions: a.Config.Actions,
TunnelServer: a.Config.TunnelServer,
}
for n, t := range a.Config.Targets {
tc := t.DeepCopy()
tc.Password = pointer.ToString("****")
nc.Targets[n] = tc
}
a.handlerCommonGet(w, nc)
}

func (a *App) handleTargetsGet(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 13a5f9e

Please sign in to comment.