Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-11470] Add human identifiable information in NodeData #6229

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

padiazg
Copy link
Member

@padiazg padiazg commented Apr 17, 2024

User description

Description

Provide IP, PID and Hostname as identifiers for a node when querying MDCB in the dataplanes endpoint. This should be tested with the MDCB version in this pr

Related Issue

https://tyktech.atlassian.net/browse/TT-11470

Motivation and Context

How This Has Been Tested

  • Run Tyk in MDCB Env
  • Enable secure endpoints in MDCB
  • Consume the /dataplanes endpoint, and now you get host details per node

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Type

enhancement, tests


Description

  • Implemented a buffered logger (BufferedLogger and BufferingFormatter) to facilitate better logging, especially for testing scenarios.
  • Enhanced the Gateway struct to include node IP address handling, which fetches and stores the IP if not provided.
  • Added comprehensive tests for new features including the retrieval and logging of node IP addresses.
  • Introduced a utility function getIpAddress to fetch the first non-loopback IPv4 address, enhancing node identification.

Changes walkthrough

Relevant files
Enhancement
buffered-logger.go
Implement Buffered Logger for Enhanced Testing                     

gateway/buffered-logger.go

  • Added a new BufferedLogger and BufferingFormatter to handle logging in
    a buffered manner, primarily for use in tests.
  • BufferedLogger includes methods to retrieve logs of a specific level.
  • +72/-0   
    server.go
    Enhance Gateway with Node IP Address Handling                       

    gateway/server.go

  • Added Address field to hostDetails struct to store node IP address.
  • Enhanced getHostDetails to fetch and store the node's IP address if
    not already provided.
  • +9/-1     
    util.go
    Add Utility Function to Fetch Non-Loopback IPv4 Address   

    gateway/util.go

  • Added getIpAddress function to retrieve the first non-loopback IPv4
    address.
  • +25/-0   
    Tests
    server_test.go
    Add Tests for Gateway Host Details and IP Address Retrieval

    gateway/server_test.go

  • Added tests for getHostDetails to verify correct logging and IP
    address retrieval.
  • Utilized BufferedLogger in tests to capture log outputs.
  • +109/-2 
    util_test.go
    Implement Tests for IP Address Retrieval Utility Function

    gateway/util_test.go

  • Added tests for getIpAddress to ensure it correctly identifies and
    returns non-loopback IPv4 addresses.
  • +82/-1   

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR Description updated to latest commit (904be81)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    4, because the PR involves multiple files and complex changes including concurrency handling, logging, and network operations which require careful review to ensure thread safety, correctness, and no memory leaks.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The BufferedLogger and BufferingFormatter use a shared instance which could lead to race conditions or data corruption if accessed concurrently without proper synchronization.

    Performance Concern: The method getIpAddress iterates over all network interfaces every time it is called, which might be inefficient, especially if called frequently.

    🔒 Security concerns

    Sensitive information exposure: The PR includes adding host details such as IP address and hostname which might expose sensitive information if logged or mishandled.

    Code feedback:
    relevant filegateway/buffered-logger.go
    suggestion      

    Consider implementing a cleanup or teardown method for BufferedLogger to properly release resources or reset the logger state between tests to prevent data leakage or interference between test cases. [important]

    relevant linevar buflogger *BufferedLogger

    relevant filegateway/server.go
    suggestion      

    Refactor getHostDetails to separate concerns, making it easier to manage and test. For example, split the function into smaller functions that handle hostname retrieval, PID file reading, and IP address retrieval independently. [important]

    relevant linegw.hostDetails.Address = gw.GetConfig().ListenAddress

    relevant filegateway/util.go
    suggestion      

    Cache the result of getIpAddress to avoid querying the network interfaces multiple times, which can improve performance especially if the IP address is needed frequently and does not change often. [important]

    relevant linefunc getIpAddress() (string, error) {

    relevant filegateway/util_test.go
    suggestion      

    Add more comprehensive tests for getIpAddress to cover scenarios such as handling multiple valid IPs, no network interfaces available, and IPv6 addresses. [medium]

    relevant linefunc Test_getIpAddress(t *testing.T) {


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Bug
    Ensure error handling for the append operation in the Format function.

    The Format function should return an error when the append operation fails. This can be
    handled by checking if the length of the buffer before and after the append operation has
    increased by one.

    gateway/buffered-logger.go [34-35]

    +originalLen := len(f.buffer)
     f.buffer = append(f.buffer, bl)
    +if len(f.buffer) != originalLen+1 {
    +  return nil, fmt.Errorf("failed to append log to buffer")
    +}
     return nil, nil
     
    Add nil check for the buffer in GetLogs to prevent nil pointer dereference.

    The GetLogs function should handle the case where the buffer is nil to prevent a potential
    nil pointer dereference.

    gateway/server.go [48-50]

    +if bl.bufferingFormatter.buffer == nil {
    +    return nil
    +}
     for _, log := range bl.bufferingFormatter.buffer {
         if log.Level == Level {
             logs = append(logs, log)
         }
     }
     
    Enhancement
    Improve error handling by returning immediately when getIpAddress fails.

    The error handling for getIpAddress should be improved by returning immediately when an
    error occurs, rather than logging and continuing execution. This prevents potential use of
    an uninitialized or default value.

    gateway/server.go [1547-1548]

     if gw.hostDetails.Address, err = getIpAddress(); err != nil {
         mainLog.Error("Failed to get node address: ", err)
    +    return
     }
     
    Improve the error message for clarity when no non-loopback address is found.

    Use a more specific error message in getHostDetails when the address is not found, to aid
    in debugging.

    gateway/server.go [1548]

    -mainLog.Error("Failed to get node address: ", err)
    +mainLog.Error("Failed to get node address, no non-loopback address found: ", err)
     
    Best practice
    Replace deprecated ioutil functions with their os package equivalents.

    Replace the direct use of ioutil.ReadFile and ioutil.WriteFile with os.ReadFile and
    os.WriteFile respectively, as ioutil package is deprecated in Go 1.16.

    gateway/server.go [1354]

    -b, err := ioutil.ReadFile(file)
    +b, err := os.ReadFile(file)
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    gateway/util.go Outdated Show resolved Hide resolved
    @titpetric
    Copy link
    Contributor

    Hey @padiazg, nice to see a contribution from your side. Could you please consider moving any extractable parts into subpackages of /internal? The PR:

    • extends internal apis
    • adds a buffered logger
    • modifies a gateway function that could be extracted

    if there's no better location, create an internal/service for the new symbols. Ideally all gateway contributions should work to not increase the size of the gateway package, separating the concerns and ensuring low coupling in a tight scope.

    Copy link

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/server_test.go b/gateway/server_test.go
    index 1d1aab4..3c32100 100644
    --- a/gateway/server_test.go
    +++ b/gateway/server_test.go
    @@ -9,11 +9,12 @@ import (
     	"testing"
     	"time"
     
    +	"github.com/sirupsen/logrus"
    +	"github.com/stretchr/testify/assert"
    +
     	"github.com/TykTechnologies/tyk/config"
     	"github.com/TykTechnologies/tyk/internal/otel"
     	"github.com/TykTechnologies/tyk/user"
    -	"github.com/sirupsen/logrus"
    -	"github.com/stretchr/testify/assert"
     )
     
     func TestGateway_afterConfSetup(t *testing.T) {
    diff --git a/gateway/util_test.go b/gateway/util_test.go
    index 6d18a1c..e50f08b 100644
    --- a/gateway/util_test.go
    +++ b/gateway/util_test.go
    @@ -6,9 +6,10 @@ import (
     	"strings"
     	"testing"
     
    +	"github.com/stretchr/testify/assert"
    +
     	"github.com/TykTechnologies/tyk/apidef"
     	"github.com/TykTechnologies/tyk/config"
    -	"github.com/stretchr/testify/assert"
     )
     
     func TestAppendIfMissingUniqueness(t *testing.T) {

    Please look at the run or in the Checks tab.

    GetIpAddres returns all addreses, IPv4 and IPv6
    Updated tests to test IPv6
    Copy link

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/server_test.go b/gateway/server_test.go
    index c7fd075..41e8cab 100644
    --- a/gateway/server_test.go
    +++ b/gateway/server_test.go
    @@ -8,12 +8,13 @@ import (
     	"testing"
     	"time"
     
    +	"github.com/sirupsen/logrus"
    +	"github.com/stretchr/testify/assert"
    +
     	"github.com/TykTechnologies/tyk/config"
     	"github.com/TykTechnologies/tyk/internal/netutil"
     	"github.com/TykTechnologies/tyk/internal/otel"
     	"github.com/TykTechnologies/tyk/user"
    -	"github.com/sirupsen/logrus"
    -	"github.com/stretchr/testify/assert"
     )
     
     func TestGateway_afterConfSetup(t *testing.T) {

    Please look at the run or in the Checks tab.

    @padiazg
    Copy link
    Member Author

    padiazg commented Apr 22, 2024

    • adds a buffered logger

    this is meant to be used only to catch the logs during tests, not to be used in any regular flow

    internal/netutil/ip-address_test.go Outdated Show resolved Hide resolved
    internal/netutil/ip-address.go Outdated Show resolved Hide resolved
    internal/log/buffered-logger.go Outdated Show resolved Hide resolved
    internal/log/buffered-logger.go Outdated Show resolved Hide resolved
    gateway/server_test.go Outdated Show resolved Hide resolved
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions bot May 9, 2024
    Copy link

    github-actions bot commented May 9, 2024

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    Copy link

    github-actions bot commented May 9, 2024

    API Changes

    --- prev.txt	2024-05-23 12:48:40.156587638 +0000
    +++ current.txt	2024-05-23 12:48:37.220569796 +0000
    @@ -733,14 +733,6 @@
                     "proxy": {
                         "type": ["object", "null"],
                         "properties": {
    -						"features": {
    -							"type": ["object", "null"],
    -							"properties": {
    -								"use_immutable_headers": {
    -									"type": "boolean"
    -								}
    -							}
    -						},
                             "auth_headers": {
                                 "type": ["object", "null"]
                             },
    @@ -1366,7 +1358,6 @@
         which will be hosted alongside the api.
     
     type GraphQLProxyConfig struct {
    -	Features              GraphQLProxyFeaturesConfig             `bson:"features" json:"features"`
     	AuthHeaders           map[string]string                      `bson:"auth_headers" json:"auth_headers"`
     	SubscriptionType      SubscriptionType                       `bson:"subscription_type" json:"subscription_type,omitempty"`
     	RequestHeaders        map[string]string                      `bson:"request_headers" json:"request_headers"`
    @@ -1374,10 +1365,6 @@
     	RequestHeadersRewrite map[string]RequestHeadersRewriteConfig `json:"request_headers_rewrite" bson:"request_headers_rewrite"`
     }
     
    -type GraphQLProxyFeaturesConfig struct {
    -	UseImmutableHeaders bool `bson:"use_immutable_headers" json:"use_immutable_headers"`
    -}
    -
     type GraphQLResponseExtensions struct {
     	OnErrorForwarding bool `bson:"on_error_forwarding" json:"on_error_forwarding"`
     }
    @@ -1605,6 +1592,7 @@
     	Tags            []string                   `json:"tags"`
     	Health          map[string]HealthCheckItem `json:"health"`
     	Stats           GWStats                    `json:"stats"`
    +	HostDetails     model.HostDetails          `json:"host_details"`
     }
     
     type NotificationsManager struct {
    @@ -11496,6 +11484,37 @@
     
     TYPES
     
    +type BufferedLog struct {
    +	Message string       `json:"message"`
    +	Time    time.Time    `json:"time"`
    +	Level   logrus.Level `json:"level"`
    +}
    +    BufferedLog is the struct that holds the log entry.
    +
    +type BufferedLogger struct {
    +	*logrus.Logger
    +	// Has unexported fields.
    +}
    +    BufferedLogger is the struct that holds the logger and the buffer.
    +
    +func NewBufferingLogger() *BufferedLogger
    +    NewBufferingLogger creates a new buffered logger.
    +
    +func (bl *BufferedLogger) ClearLogs()
    +    ClearLogs clears the logs from the buffer. IMPORTANT: Must be called before
    +    each test iteration.
    +
    +func (bl *BufferedLogger) GetLogs(Level logrus.Level) []*BufferedLog
    +    GetLogs returns the logs that are stored in the buffer.
    +
    +type BufferingFormatter struct {
    +	// Has unexported fields.
    +}
    +    BufferingFormatter is the struct that holds the buffer of the logs.
    +
    +func (f *BufferingFormatter) Format(entry *logrus.Entry) ([]byte, error)
    +    Format is the method that stores the log entry in the buffer.
    +
     type ClientOption func(*http.Client)
         Options for populating a http.Client
     

    Copy link

    github-actions bot commented May 9, 2024

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    Copy link

    github-actions bot commented May 9, 2024

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    Copy link

    github-actions bot commented May 9, 2024

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    internal/models/host_details.go Outdated Show resolved Hide resolved
    @@ -1540,6 +1540,17 @@ func (gw *Gateway) getHostDetails(file string) {
    if gw.hostDetails.Hostname, err = os.Hostname(); err != nil {
    mainLog.Error("Failed to get hostname: ", err)
    }

    gw.hostDetails.Address = gw.GetConfig().ListenAddress
    if gw.hostDetails.Address == "" {
    Copy link
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Not sure this is correct. Wouldn't listenaddress be set to 0.0.0.0 to listen on all interfaces by default?

    Copy link
    Member Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    probably, but that was on the original code: https://github.com/TykTechnologies/tyk/blob/master/gateway/server.go#L1923
    I just changed gw.GetConfig().ListenAddress for gw.hostDetails.Address not to call the function twice.

    gateway/server_test.go Outdated Show resolved Hide resolved
    gateway/server_test.go Outdated Show resolved Hide resolved
    internal/netutil/ip_address_test.go Outdated Show resolved Hide resolved
    @padiazg padiazg requested a review from titpetric May 17, 2024 13:58
    Copy link
    Contributor

    @titpetric titpetric left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Mostly reviewed for the code structure conventions, models/ needs a rename, everything else LGTM

    Copy link
    Contributor

    @sredxny sredxny left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    looks good! Only address the comment above from Tit

    Copy link

    💥 CI tests failed 🙈

    git-state

    diff --git a/gateway/server.go b/gateway/server.go
    index 11e0676..0f7a00c 100644
    --- a/gateway/server.go
    +++ b/gateway/server.go
    @@ -25,6 +25,7 @@ import (
     	textTemplate "text/template"
     	"time"
     
    +	"github.com/TykTechnologies/storage/temporal/model"
     	"github.com/TykTechnologies/tyk/internal/crypto"
     	"github.com/TykTechnologies/tyk/internal/httputil"
     	"github.com/TykTechnologies/tyk/internal/otel"

    Please look at the run or in the Checks tab.

    Copy link

    sonarcloud bot commented May 23, 2024

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    3 participants