diff --git a/ai/client.go b/ai/client.go index 72973b34..fdfc27b3 100644 --- a/ai/client.go +++ b/ai/client.go @@ -7,7 +7,6 @@ import ( "io" "net/http" "net/url" - "os" "path" "strings" @@ -33,7 +32,7 @@ type Config struct { OrganizationID string `Validate:"required"` BaseURL string Service string `Validate:"required"` - DebugLog string + DebugLog io.Writer Retry int } @@ -47,8 +46,7 @@ type Client struct { // User agent used when communicating with the HSDP Notification API UserAgent string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate ComputeTarget *ComputeTargetService ComputeProvider *ComputeProviderService @@ -89,10 +87,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // GetBaseURL returns the base URL as configured diff --git a/ai/client_test.go b/ai/client_test.go index 6c2870e2..60819dfd 100644 --- a/ai/client_test.go +++ b/ai/client_test.go @@ -171,7 +171,7 @@ func TestDebug(t *testing.T) { aiClient, err = ai.NewClient(iamClient, &ai.Config{ BaseURL: serverAI.URL, - DebugLog: tempFile.Name(), + DebugLog: tempFile, Service: "inference", OrganizationID: "xxx", }) diff --git a/ai/inference/client_test.go b/ai/inference/client_test.go index 954db134..cfe0686b 100644 --- a/ai/inference/client_test.go +++ b/ai/inference/client_test.go @@ -206,7 +206,7 @@ func TestDebug(t *testing.T) { inferenceClient, err = inference.NewClient(iamClient, &ai.Config{ BaseURL: serverInference.URL, - DebugLog: tempFile.Name(), + DebugLog: tempFile, OrganizationID: "xxx", }) if !assert.Nil(t, err) { diff --git a/ai/training/client_test.go b/ai/training/client_test.go index 14e2c3ef..e05a6fb7 100644 --- a/ai/training/client_test.go +++ b/ai/training/client_test.go @@ -206,7 +206,7 @@ func TestDebug(t *testing.T) { trainingClient, err = training.NewClient(iamClient, &ai.Config{ BaseURL: serverTraining.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, OrganizationID: "xxx", }) if !assert.Nil(t, err) { diff --git a/ai/workspace/client_test.go b/ai/workspace/client_test.go index 6b3e3a5b..5a46d8c0 100644 --- a/ai/workspace/client_test.go +++ b/ai/workspace/client_test.go @@ -205,7 +205,7 @@ func TestDebug(t *testing.T) { workspaceClient, err = workspace.NewClient(iamClient, &ai.Config{ BaseURL: serverWorkspace.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, OrganizationID: "xxx", }) if !assert.Nil(t, err) { diff --git a/audit/client.go b/audit/client.go index 2a82002c..ec431338 100644 --- a/audit/client.go +++ b/audit/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/google/fhir/go/fhirversion" @@ -38,7 +37,7 @@ type Config struct { // SharedSecret is the IAM API signing secret SharedSecret string TimeZone string - DebugLog string + DebugLog io.Writer } // Client holds state of a HSDP Audit client @@ -53,8 +52,6 @@ type Client struct { ma *jsonformat.Marshaller um *jsonformat.Unmarshaller httpSigner *signer.Signer - - debugFile *os.File } // NewClient returns a new HSDP Audit API client. Configured console and IAM clients @@ -76,12 +73,8 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { } c := &Client{httpClient: httpClient, config: config, UserAgent: userAgent} - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } c.httpSigner, err = signer.New(c.config.SharedKey, c.config.SharedSecret) if err != nil { @@ -104,10 +97,6 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } func (c *Client) setAuditBaseURL(urlStr string) error { diff --git a/blr/client.go b/blr/client.go index 3f200664..15ba57c9 100644 --- a/blr/client.go +++ b/blr/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "path" "strings" @@ -32,7 +31,7 @@ type Config struct { Region string Environment string BaseURL string - DebugLog string + DebugLog io.Writer Retry int } @@ -46,8 +45,7 @@ type Client struct { // User agent used when communicating with the HSDP Blob Repository API UserAgent string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate Blobs *BlobsService } @@ -89,10 +87,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // GetBaseURL returns the base URL as configured diff --git a/cartel/client.go b/cartel/client.go index 375ed70a..857f60bd 100644 --- a/cartel/client.go +++ b/cartel/client.go @@ -12,7 +12,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/philips-software/go-hsdp-api/internal" @@ -28,14 +27,13 @@ const ( // Config the client type Config struct { - Region string `cloud:"-" json:"-"` - Token string `cloud:"token" json:"token"` - Secret string `cloud:"secret" json:"secret"` - SkipVerify bool `cloud:"skip_verify" json:"skip_verify"` - NoTLS bool `cloud:"no_tls" json:"no_tls"` - Host string `cloud:"host" json:"host"` - Debug bool `cloud:"-" json:"debug,omitempty"` - DebugLog string `cloud:"-" json:"debug_log,omitempty"` + Region string `cloud:"-" json:"-"` + Token string `cloud:"token" json:"token"` + Secret string `cloud:"secret" json:"secret"` + SkipVerify bool `cloud:"skip_verify" json:"skip_verify"` + NoTLS bool `cloud:"no_tls" json:"no_tls"` + Host string `cloud:"host" json:"host"` + DebugLog io.Writer `cloud:"-" json:"-"` } // Valid returns if all required config fields are present, false otherwise @@ -58,8 +56,6 @@ type Client struct { httpClient *http.Client baseURL *url.URL userAgent string - - debugFile *os.File } // Response holds a Cartel response @@ -121,12 +117,8 @@ func NewClient(httpClient *http.Client, config *Config) (*Client, error) { cartel.httpClient = httpClient cartel.userAgent = userAgent - if config.DebugLog != "" { - var err error - cartel.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, cartel.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } // Make sure the given URL ends with a slash diff --git a/cartel/client_test.go b/cartel/client_test.go index 733aabcb..2d5c4cb1 100644 --- a/cartel/client_test.go +++ b/cartel/client_test.go @@ -77,8 +77,7 @@ func TestDebug(t *testing.T) { Token: sharedToken, Secret: sharedSecret, Host: "foo", - Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/cdl/client.go b/cdl/client.go index 90dcaa0b..bb09e009 100644 --- a/cdl/client.go +++ b/cdl/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/go-playground/validator/v10" @@ -33,7 +32,7 @@ type Config struct { OrganizationID string `validate:"required"` CDLURL string CDLStore string - DebugLog string + DebugLog io.Writer Retry int } @@ -51,8 +50,7 @@ type Client struct { // User agent used when communicating with the HSDP Notification API UserAgent string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate Study *StudyService DataTypeDefinition *DatatypeDefinitionService @@ -128,10 +126,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // GetCDLURL returns the base CDL Store base URL as configured diff --git a/cdl/client_test.go b/cdl/client_test.go index 1cfca2b6..5ac8a1b8 100644 --- a/cdl/client_test.go +++ b/cdl/client_test.go @@ -167,7 +167,7 @@ func TestDebug(t *testing.T) { cdlClient, err = cdl.NewClient(iamClient, &cdl.Config{ CDLStore: serverCDL.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/cdr/client.go b/cdr/client.go index 7415b93d..722f80cb 100644 --- a/cdr/client.go +++ b/cdr/client.go @@ -36,7 +36,7 @@ type Config struct { FHIRStore string Type string TimeZone string - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP CDR API diff --git a/cdr/client_test.go b/cdr/client_test.go index a6e2ceb4..117716f6 100644 --- a/cdr/client_test.go +++ b/cdr/client_test.go @@ -185,7 +185,7 @@ func TestDebug(t *testing.T) { cdrClient, err = cdr.NewClient(iamClient, &cdr.Config{ CDRURL: serverCDR.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/connect/mdm/client.go b/connect/mdm/client.go index 5fc369f9..be6471ac 100644 --- a/connect/mdm/client.go +++ b/connect/mdm/client.go @@ -7,7 +7,6 @@ import ( "io" "net/http" "net/url" - "os" "path" "strings" @@ -31,7 +30,7 @@ type Config struct { Region string Environment string BaseURL string - DebugLog string + DebugLog io.Writer Retry int } @@ -48,8 +47,7 @@ type Client struct { systemIDM string systemIAM string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate Propositions *PropositionsService Applications *ApplicationsService @@ -142,10 +140,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // GetBaseURL returns the base URL as configured diff --git a/console/client.go b/console/client.go index 032ec866..f9a755e7 100644 --- a/console/client.go +++ b/console/client.go @@ -9,7 +9,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "sync" "time" @@ -131,7 +130,6 @@ type Client struct { Metrics *MetricsService - debugFile *os.File consoleErr error sync.Mutex @@ -166,24 +164,16 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { if err := c.SetBaseConsoleURL(c.config.BaseConsoleURL); err != nil { c.consoleErr = err } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } header := make(http.Header) header.Set("User-Agent", userAgent) httpClient.Transport = internal.NewHeaderRoundTripper(httpClient.Transport, header) authClient := oauth2.NewClient(context.Background(), c) - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - authClient.Transport = internal.NewLoggingRoundTripper(authClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + authClient.Transport = internal.NewLoggingRoundTripper(authClient.Transport, config.DebugLog) } // Injecting these headers so we satisfy the proxies authClient.Transport = internal.NewHeaderRoundTripper(authClient.Transport, header, func(req *http.Request) error { @@ -221,10 +211,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // HttpClient returns the http Client used for connections diff --git a/console/client_test.go b/console/client_test.go index 19418756..98a5df76 100644 --- a/console/client_test.go +++ b/console/client_test.go @@ -136,7 +136,7 @@ func TestDebug(t *testing.T) { UAAURL: serverUAA.URL, BaseConsoleURL: serverCONSOLE.URL, Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if err != nil { diff --git a/console/config.go b/console/config.go index 6b0e71b8..a63a03c0 100644 --- a/console/config.go +++ b/console/config.go @@ -1,5 +1,7 @@ package console +import "io" + // Config contains the configuration of a client type Config struct { Region string @@ -8,5 +10,5 @@ type Config struct { MetricsAPIURL string Scopes []string Debug bool - DebugLog string + DebugLog io.Writer } diff --git a/console/docker/client.go b/console/docker/client.go index 58515b67..5810a3b9 100644 --- a/console/docker/client.go +++ b/console/docker/client.go @@ -3,8 +3,8 @@ package docker import ( "context" + "io" "net/http" - "os" "github.com/hasura/go-graphql-client" autoconf "github.com/philips-software/go-hsdp-api/config" @@ -23,7 +23,7 @@ type OptionFunc func(*http.Request) error type Config struct { Region string DockerAPIURL string - DebugLog string + DebugLog io.Writer host string } @@ -39,8 +39,6 @@ type Client struct { // User agent used when communicating with the HSDP DICOM API. UserAgent string - debugFile *os.File - ServiceKeys *ServiceKeysService Namespaces *NamespacesService Repositories *RepositoriesService @@ -105,8 +103,4 @@ func (c *Client) Query(ctx context.Context, q interface{}, variables map[string] // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } diff --git a/console/docker/client_test.go b/console/docker/client_test.go index 2883ecbb..0676691c 100644 --- a/console/docker/client_test.go +++ b/console/docker/client_test.go @@ -46,7 +46,7 @@ func setup(t *testing.T) (func(), error) { consoleClient, err = console.NewClient(nil, &console.Config{ UAAURL: serverUAA.URL, BaseConsoleURL: serverCONSOLE.URL, - DebugLog: tmpFile.Name(), + DebugLog: tmpFile, }) if !assert.Nil(t, err) { t.Fatalf("invalid consoleClient") diff --git a/dicom/client.go b/dicom/client.go index acf6bcb5..e98b6a7b 100644 --- a/dicom/client.go +++ b/dicom/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/google/fhir/go/fhirversion" @@ -36,7 +35,7 @@ type Config struct { DICOMConfigURL string Type string TimeZone string - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP DICOM API @@ -51,8 +50,6 @@ type Client struct { // User agent used when communicating with the HSDP DICOM API. UserAgent string - debugFile *os.File - Config *ConfigService } @@ -85,10 +82,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } func (c *Client) GetSTOWURL() string { diff --git a/dicom/client_test.go b/dicom/client_test.go index a12a1049..0c223e0d 100644 --- a/dicom/client_test.go +++ b/dicom/client_test.go @@ -169,7 +169,7 @@ func TestDebug(t *testing.T) { dicomClient, err = dicom.NewClient(iamClient, &dicom.Config{ DICOMConfigURL: serverDICOM.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/discovery/client.go b/discovery/client.go index 2932140c..9230a1fb 100644 --- a/discovery/client.go +++ b/discovery/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "path" "strings" @@ -32,7 +31,7 @@ type Config struct { Region string Environment string BaseURL string - DebugLog string + DebugLog io.Writer Retry int } @@ -46,8 +45,7 @@ type Client struct { // User agent used when communicating with the HSDP Notification API UserAgent string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate } // Service describes a discovered service @@ -93,10 +91,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // GetBaseURL returns the base URL as configured diff --git a/discovery/client_test.go b/discovery/client_test.go index 7aa513bb..a54b141d 100644 --- a/discovery/client_test.go +++ b/discovery/client_test.go @@ -171,7 +171,7 @@ func TestDebug(t *testing.T) { aiClient, err = ai.NewClient(iamClient, &ai.Config{ BaseURL: serverAI.URL, - DebugLog: tempFile.Name(), + DebugLog: tempFile, Service: "inference", OrganizationID: "xxx", }) diff --git a/has/client.go b/has/client.go index 41bd5abb..e2614588 100644 --- a/has/client.go +++ b/has/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "sort" "strings" @@ -30,7 +29,7 @@ type Config struct { HASURL string OrgID string Debug bool - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP IAM API @@ -45,8 +44,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Resources *ResourcesService Sessions *SessionsService Images *ImagesService @@ -68,13 +65,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { "HAS_SESSION.ALL", "HAS_RESOURCE.ALL") { return nil, ErrMissingHASPermissions } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - c.debugFile = nil - } - } c.Resources = &ResourcesService{client: c, orgID: config.OrgID} c.Sessions = &SessionsService{client: c, orgID: config.OrgID} @@ -84,10 +74,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBaseHASURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/has/client_test.go b/has/client_test.go index d345a432..018b2cf2 100644 --- a/has/client_test.go +++ b/has/client_test.go @@ -174,7 +174,7 @@ func TestDebug(t *testing.T) { HASURL: serverHAS.URL, Debug: true, OrgID: orgID, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/iam/client.go b/iam/client.go index f1fd80c9..3b40adfa 100644 --- a/iam/client.go +++ b/iam/client.go @@ -9,7 +9,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "sync" "time" @@ -76,8 +75,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Organizations *OrganizationsService Groups *GroupsService Permissions *PermissionsService @@ -129,12 +126,8 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { } else { c.signer = config.Signer } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } c.validate = validator.New() @@ -184,10 +177,6 @@ func (c *Client) validSigner() bool { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // HttpClient returns the http Client used for connections diff --git a/iam/client_test.go b/iam/client_test.go index 57646b2f..4505cf52 100644 --- a/iam/client_test.go +++ b/iam/client_test.go @@ -443,8 +443,7 @@ func TestDebug(t *testing.T) { SecretKey: secretKey, IAMURL: serverIAM.URL, IDMURL: serverIDM.URL, - Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if err != nil { diff --git a/iam/config.go b/iam/config.go index 2ffefff3..5d0f32d9 100644 --- a/iam/config.go +++ b/iam/config.go @@ -1,6 +1,8 @@ package iam import ( + "io" + hsdpsigner "github.com/philips-software/go-hsdp-signer" ) @@ -20,7 +22,6 @@ type Config struct { IDMURL string Scopes []string RootOrgID string - Debug bool - DebugLog string + DebugLog io.Writer Signer *hsdpsigner.Signer } diff --git a/internal/round_trippers.go b/internal/round_trippers.go index a0ab30a5..f6a39c43 100644 --- a/internal/round_trippers.go +++ b/internal/round_trippers.go @@ -2,6 +2,7 @@ package internal import ( "fmt" + "io" "net/http" "net/http/httputil" "os" @@ -69,22 +70,22 @@ func (rt *HeaderRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, } type LoggingRoundTripper struct { - next http.RoundTripper - logFile *os.File - id int64 - prefix string - debug bool + next http.RoundTripper + w io.Writer + id int64 + prefix string + debug bool } -func NewLoggingRoundTripper(next http.RoundTripper, logFile *os.File) *LoggingRoundTripper { +func NewLoggingRoundTripper(next http.RoundTripper, w io.Writer) *LoggingRoundTripper { if next == nil { next = http.DefaultTransport } return &LoggingRoundTripper{ - next: next, - logFile: logFile, - prefix: uuid.New().String(), - debug: os.Getenv(Amos) == "true", + next: next, + w: w, + prefix: uuid.New().String(), + debug: os.Getenv(Amos) == "true", } } @@ -94,7 +95,7 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response rt.id++ id := fmt.Sprintf("%s-%05d", rt.prefix, localID) - if rt.logFile != nil { + if rt.w != nil { now := time.Now().UTC().Format(time.RFC3339Nano) out := "" dumped, err := httputil.DumpRequest(req, true) @@ -109,7 +110,7 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response } else { out = fmt.Sprintf("[go-hsdp-api %s %s] --- request start ---\n%s\n[go-hsdp-api %s %s] --- request end ---\n", id, now, filtered, id, now) } - _, _ = rt.logFile.WriteString(out) + _, _ = io.WriteString(rt.w, out) } resp, err = rt.next.RoundTrip(req) @@ -117,7 +118,7 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response return resp, err } - if rt.logFile != nil { + if rt.w != nil { now := time.Now().UTC().Format(time.RFC3339Nano) out := "" dumped, err := httputil.DumpResponse(resp, true) @@ -132,7 +133,7 @@ func (rt *LoggingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response } else { out = fmt.Sprintf("[go-hsdp-api %s %s] --- response start ---\n%s\n[go-hsdp-api %s %s] --- response end ---\n", id, now, filtered, id, now) } - _, _ = rt.logFile.WriteString(out) + _, _ = io.WriteString(rt.w, out) } return resp, err diff --git a/iron/client.go b/iron/client.go index 78ef353c..3e86e4c4 100644 --- a/iron/client.go +++ b/iron/client.go @@ -7,7 +7,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/philips-software/go-hsdp-api/internal" @@ -27,7 +26,7 @@ type OptionFunc func(*http.Request) error type Config struct { BaseURL string `cloud:"-" json:"base_url,omitempty"` Debug bool `cloud:"-" json:"-"` - DebugLog string `cloud:"-" json:"-"` + DebugLog io.Writer `cloud:"-" json:"-"` ClusterInfo []ClusterInfo `cloud:"cluster_info" json:"cluster_info"` Email string `cloud:"email" json:"email"` Password string `cloud:"password" json:"password"` @@ -56,8 +55,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Tasks *TasksServices Codes *CodesServices Clusters *ClustersServices @@ -85,12 +82,8 @@ func newClient(config *Config) (*Client, error) { if err := c.SetBaseIronURL(useURL); err != nil { return nil, err } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } c.Tasks = &TasksServices{client: c, projectID: config.ProjectID} @@ -109,10 +102,6 @@ func (c ClusterInfo) Encrypt(payload []byte) (string, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBaseIronURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/iron/client_test.go b/iron/client_test.go index 34153e04..6c71e664 100644 --- a/iron/client_test.go +++ b/iron/client_test.go @@ -28,8 +28,6 @@ func setup(t *testing.T) func() { BaseURL: serverIRON.URL, ProjectID: projectID, Token: token, - Debug: true, - DebugLog: "/tmp/iron_test.log", }) assert.Nil(t, err) assert.NotNil(t, client) diff --git a/logging/client.go b/logging/client.go index bbada201..3223b240 100644 --- a/logging/client.go +++ b/logging/client.go @@ -87,7 +87,7 @@ type Config struct { BaseURL string ProductKey string Debug bool - DebugLog string + DebugLog io.Writer } // Valid returns if all required config fields are present, false otherwise @@ -114,7 +114,6 @@ type Client struct { url *url.URL httpClient *http.Client httpSigner *signer.Signer - debugFile *os.File } // StoreResponse holds a LogEvent response @@ -149,15 +148,8 @@ func NewClient(httpClient *http.Client, config *Config) (*Client, error) { } httpClient = c } - if config.DebugLog != "" || config.Debug { - var err error - if config.DebugLog == "" { // Simulate original behaviour - config.DebugLog = "/dev/stderr" - } - logger.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, logger.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } // Autoconfig if config.Region != "" && config.Environment != "" { diff --git a/logging/integration_test.go b/logging/integration_test.go index a46030e5..9192168c 100644 --- a/logging/integration_test.go +++ b/logging/integration_test.go @@ -49,7 +49,6 @@ func TestIntegration(t *testing.T) { SharedSecret: secret, ProductKey: productKey, BaseURL: ingestorURL, - DebugLog: "/tmp/integration.log", }) if !assert.Nil(t, err) { return diff --git a/notification/client.go b/notification/client.go index 20648ec8..736b54fa 100644 --- a/notification/client.go +++ b/notification/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/go-playground/validator/v10" @@ -34,7 +33,7 @@ type Config struct { NotificationURL string Type string TimeZone string - DebugLog string + DebugLog io.Writer Retry int } @@ -50,8 +49,7 @@ type Client struct { // User agent used when communicating with the HSDP Notification API UserAgent string - debugFile *os.File - validate *validator.Validate + validate *validator.Validate Producer *ProducerService Subscription *SubscriptionService @@ -97,10 +95,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetNotificationURL sets the Notification URL for API requests diff --git a/notification/client_test.go b/notification/client_test.go index 830e332d..b7dd8de9 100644 --- a/notification/client_test.go +++ b/notification/client_test.go @@ -166,7 +166,7 @@ func TestDebug(t *testing.T) { notificationClient, err = notification.NewClient(iamClient, ¬ification.Config{ NotificationURL: serverNotification.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if !assert.Nil(t, err) { return diff --git a/pki/client.go b/pki/client.go index d6bd3f19..6da2df63 100644 --- a/pki/client.go +++ b/pki/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "sort" "strings" @@ -39,7 +38,7 @@ type Config struct { Environment string PKIURL string UAAURL string - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP PKI API @@ -56,8 +55,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Tenants *TenantService Services *ServicesService // Sounds like something from Java! } @@ -75,13 +72,6 @@ func newClient(consoleClient *console.Client, iamClient *iam.Client, config *Con return nil, err } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - c.debugFile = nil - } - } c.Tenants = &TenantService{client: c, validate: validator.New()} c.Services = &ServicesService{client: c, validate: validator.New()} return c, nil @@ -108,10 +98,6 @@ func doAutoconf(config *Config) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBasePKIURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/pki/client_test.go b/pki/client_test.go index 4911db96..3c7a6aa9 100644 --- a/pki/client_test.go +++ b/pki/client_test.go @@ -221,7 +221,7 @@ func TestDebug(t *testing.T) { pkiClient, err = pki.NewClient(nil, iamClient, &pki.Config{ PKIURL: serverPKI.URL, UAAURL: serverUAA.URL, - DebugLog: tmpFile.Name(), + DebugLog: tmpFile, }) if !assert.Nil(t, err) { return diff --git a/s3creds/client.go b/s3creds/client.go index 2c755e65..fe01d866 100644 --- a/s3creds/client.go +++ b/s3creds/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "sort" "strings" @@ -32,8 +31,7 @@ type Config struct { BaseURL string Region string Environment string - Debug bool - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP IAM API @@ -48,8 +46,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Policy *PolicyService Access *AccessService } @@ -83,13 +79,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { if err := c.SetBaseURL(c.config.BaseURL); err != nil { return nil, err } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - c.debugFile = nil - } - } c.Policy = &PolicyService{client: c, validate: validator.New()} _ = c.Policy.validate.RegisterValidation("policyActions", validateActions) @@ -100,10 +89,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBaseURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/s3creds/client_test.go b/s3creds/client_test.go index 08a06c55..2ced758d 100644 --- a/s3creds/client_test.go +++ b/s3creds/client_test.go @@ -104,8 +104,7 @@ func TestDebug(t *testing.T) { credsClient, err = NewClient(iamClient, &Config{ BaseURL: serverCreds.URL, - Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if err != nil { t.Fatalf("Error: %v", err) diff --git a/stl/client.go b/stl/client.go index 18eb39b1..5c947f4f 100644 --- a/stl/client.go +++ b/stl/client.go @@ -3,8 +3,8 @@ package stl import ( "context" + "io" "net/http" - "os" "github.com/hasura/go-graphql-client" autoconf "github.com/philips-software/go-hsdp-api/config" @@ -25,7 +25,7 @@ type Config struct { Region string Environment string STLAPIURL string - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP Edge API @@ -40,8 +40,6 @@ type Client struct { // User agent used when communicating with the HSDP Edge API. UserAgent string - debugFile *os.File - Devices *DevicesService Apps *AppsService Config *ConfigService @@ -59,12 +57,8 @@ func newClient(consoleClient *console.Client, config *Config) (*Client, error) { c := &Client{consoleClient: consoleClient, config: config, UserAgent: userAgent} httpClient := oauth2.NewClient(context.Background(), consoleClient) - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } header := make(http.Header) header.Set("User-Agent", userAgent) @@ -99,8 +93,4 @@ func (c *Client) Query(ctx context.Context, q interface{}, variables map[string] // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } diff --git a/stl/client_test.go b/stl/client_test.go index a6634adf..00e62912 100644 --- a/stl/client_test.go +++ b/stl/client_test.go @@ -105,7 +105,7 @@ func TestDebug(t *testing.T) { client, err = stl.NewClient(consoleClient, &stl.Config{ STLAPIURL: serverSTL.URL, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if err != nil { diff --git a/tdr/client.go b/tdr/client.go index 6c6e0f17..ca489fac 100644 --- a/tdr/client.go +++ b/tdr/client.go @@ -9,7 +9,6 @@ import ( "io" "net/http" "net/url" - "os" "sort" "strings" @@ -30,7 +29,7 @@ type OptionFunc func(*http.Request) error type Config struct { TDRURL string Debug bool - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP IAM API @@ -45,8 +44,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Contracts *ContractsService DataItems *DataItemsService } @@ -66,13 +63,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { if !iamClient.HasScopes("tdr.contract", "tdr.dataitem") { return nil, ErrMissingTDRScopes } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - c.debugFile = nil - } - } c.Contracts = &ContractsService{client: c} c.DataItems = &DataItemsService{client: c} @@ -81,10 +71,6 @@ func newClient(iamClient *iam.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBaseTDRURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/tdr/client_test.go b/tdr/client_test.go index 16bef6b0..2e47e91a 100644 --- a/tdr/client_test.go +++ b/tdr/client_test.go @@ -107,7 +107,7 @@ func TestDebug(t *testing.T) { tdrClient, err = NewClient(iamClient, &Config{ TDRURL: serverTDR.URL, Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) if err != nil { t.Fatalf("Error: %v", err) diff --git a/tpns/client.go b/tpns/client.go index d88d7fc6..805cdfae 100644 --- a/tpns/client.go +++ b/tpns/client.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "net/url" - "os" "strings" "github.com/philips-software/go-hsdp-api/internal" @@ -30,7 +29,7 @@ type Config struct { Username string Password string Debug bool - DebugLog string + DebugLog io.Writer } // A Client manages communication with HSDP IAM API @@ -45,8 +44,6 @@ type Client struct { // User agent used when communicating with the HSDP IAM API. UserAgent string - debugFile *os.File - Messages *MessagesService } @@ -70,12 +67,8 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { return nil, err } - if config.DebugLog != "" { - var err error - c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err == nil { - httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile) - } + if config.DebugLog != nil { + httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog) } c.Messages = &MessagesService{client: c} @@ -84,10 +77,6 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) { // Close releases allocated resources of clients func (c *Client) Close() { - if c.debugFile != nil { - _ = c.debugFile.Close() - c.debugFile = nil - } } // SetBaseTPNSURL sets the base URL for API requests to a custom endpoint. urlStr diff --git a/tpns/client_test.go b/tpns/client_test.go index 13d1acfe..94b2d33e 100644 --- a/tpns/client_test.go +++ b/tpns/client_test.go @@ -91,7 +91,7 @@ func TestDebug(t *testing.T) { tpnsClient, _ = NewClient(nil, &Config{ TPNSURL: serverTPNS.URL, Debug: true, - DebugLog: tmpfile.Name(), + DebugLog: tmpfile, }) defer func() { tpnsClient.Close()