From a0b2d84d75b2422e7eda3564315166617ee72b31 Mon Sep 17 00:00:00 2001 From: "Y.Horie" Date: Wed, 28 Jun 2023 19:44:17 +0900 Subject: [PATCH 1/4] Ignore directives feature (#343) Ignore directives feature --- sdk/config_apply.go | 6 ++++-- sdk/config_apply_test.go | 10 ++++++++-- sdk/config_helpers.go | 11 ++++++++--- sdk/config_helpers_test.go | 14 +++++++++----- src/core/config/config.go | 1 + src/core/config/defaults.go | 19 +++++++++++++------ src/core/config/types.go | 1 + src/core/environment_test.go | 8 ++++---- src/core/nginx.go | 9 +++++---- .../nginx-app-protect/nap/nap_metadata.go | 3 ++- .../nap/nap_metadata_test.go | 6 ++++-- src/plugins/agent_api.go | 2 +- src/plugins/metrics.go | 2 +- src/plugins/nginx.go | 2 +- src/plugins/nginx_test.go | 9 ++++++--- .../nginx/agent/sdk/v2/config_apply.go | 6 ++++-- .../nginx/agent/sdk/v2/config_helpers.go | 11 ++++++++--- .../nginx/agent/sdk/v2/config_apply.go | 6 ++++-- .../nginx/agent/sdk/v2/config_helpers.go | 11 ++++++++--- .../nginx/agent/v2/src/core/config/config.go | 1 + .../agent/v2/src/core/config/defaults.go | 19 +++++++++++++------ .../nginx/agent/v2/src/core/config/types.go | 1 + .../nginx/agent/v2/src/core/nginx.go | 9 +++++---- .../nginx-app-protect/nap/nap_metadata.go | 3 ++- .../nginx/agent/v2/src/plugins/agent_api.go | 2 +- .../nginx/agent/v2/src/plugins/metrics.go | 2 +- .../nginx/agent/v2/src/plugins/nginx.go | 2 +- .../nginx/agent/sdk/v2/config_apply.go | 6 ++++-- .../nginx/agent/sdk/v2/config_helpers.go | 11 ++++++++--- 29 files changed, 129 insertions(+), 64 deletions(-) diff --git a/sdk/config_apply.go b/sdk/config_apply.go index 00660591bd..3df4d29fb4 100644 --- a/sdk/config_apply.go +++ b/sdk/config_apply.go @@ -35,6 +35,7 @@ type ConfigApply struct { func NewConfigApply( confFile string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*ConfigApply, error) { w, err := zip.NewWriter("/") if err != nil { @@ -47,7 +48,7 @@ func NewConfigApply( notExistDirs: make(map[string]struct{}), } if confFile != "" { - return b, b.mapCurrentFiles(confFile, allowedDirectories) + return b, b.mapCurrentFiles(confFile, allowedDirectories, ignoreDirectives) } return b, nil } @@ -179,10 +180,11 @@ func (b *ConfigApply) RemoveFromNotExists(fullPath string) { // mapCurrentFiles parse the provided file via cross-plane, generate a list of files, which should be identical to the // DirectoryMap, will mark off the files as the config is being applied, any leftovers after complete should be deleted. -func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}) error { +func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string) error { log.Debugf("parsing %s", confFile) payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/sdk/config_apply_test.go b/sdk/config_apply_test.go index a34e8de2bc..0ec126ba03 100644 --- a/sdk/config_apply_test.go +++ b/sdk/config_apply_test.go @@ -98,6 +98,7 @@ func TestNewConfigApply(t *testing.T) { name string confFile string allowedDirectories map[string]struct{} + ignoreDirectives []string expectedConfigApply *ConfigApply expectError bool }{ @@ -107,6 +108,7 @@ func TestNewConfigApply(t *testing.T) { allowedDirectories: map[string]struct{}{ tmpDir: {}, }, + ignoreDirectives: []string{}, expectedConfigApply: &ConfigApply{ existing: map[string]struct{}{ defaultConfFile: {}, @@ -124,6 +126,7 @@ func TestNewConfigApply(t *testing.T) { name: "no config file present", confFile: "", allowedDirectories: map[string]struct{}{}, + ignoreDirectives: []string{}, expectedConfigApply: &ConfigApply{ existing: map[string]struct{}{}, notExists: map[string]struct{}{}, @@ -135,6 +138,7 @@ func TestNewConfigApply(t *testing.T) { name: "empty config file present", confFile: emptyConfFile, allowedDirectories: map[string]struct{}{}, + ignoreDirectives: []string{}, expectedConfigApply: &ConfigApply{ existing: map[string]struct{}{}, notExists: map[string]struct{}{}, @@ -146,6 +150,7 @@ func TestNewConfigApply(t *testing.T) { name: "unknown config file present", confFile: "/tmp/unknown.conf", allowedDirectories: map[string]struct{}{}, + ignoreDirectives: []string{}, expectedConfigApply: &ConfigApply{ existing: map[string]struct{}{}, notExists: map[string]struct{}{}, @@ -157,7 +162,7 @@ func TestNewConfigApply(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - configApply, err := NewConfigApply(tc.confFile, tc.allowedDirectories) + configApply, err := NewConfigApply(tc.confFile, tc.allowedDirectories, tc.ignoreDirectives) assert.Equal(t, tc.expectedConfigApply.existing, configApply.GetExisting()) assert.Equal(t, tc.expectedConfigApply.notExists, configApply.GetNotExists()) assert.Equal(t, tc.expectedConfigApply.notExistDirs, configApply.GetNotExistDirs()) @@ -266,8 +271,9 @@ func TestConfigApplyCompleteAndRollback(t *testing.T) { require.NoError(t, os.WriteFile(confFile, []byte(confFileContent), 0644)) allowedDirectories := map[string]struct{}{tmpDir: {}} + ignoreDirectives := []string{} - configApply, err := NewConfigApply(confFile, allowedDirectories) + configApply, err := NewConfigApply(confFile, allowedDirectories, ignoreDirectives) assert.Equal(t, 5, len(configApply.GetExisting())) assert.Nil(t, err) diff --git a/sdk/config_helpers.go b/sdk/config_helpers.go index 940d401770..0635b4ebc7 100644 --- a/sdk/config_helpers.go +++ b/sdk/config_helpers.go @@ -100,9 +100,11 @@ func GetNginxConfig( nginxId, systemId string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*proto.NginxConfig, error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -735,9 +737,10 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string) (statusApi string, err error) { +func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, CombineConfigs: true, @@ -756,7 +759,7 @@ func GetStatusApiInfo(confFile string) (statusApi string, err error) { return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -770,6 +773,7 @@ func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -835,7 +839,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -845,6 +849,7 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, [ payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/sdk/config_helpers_test.go b/sdk/config_helpers_test.go index 84a7510e42..2d23305701 100644 --- a/sdk/config_helpers_test.go +++ b/sdk/config_helpers_test.go @@ -633,12 +633,13 @@ func TestGetNginxConfig(t *testing.T) { assert.NoError(t, err) allowedDirs := map[string]struct{}{} + ignoreDirectives := []string{} if test.expected.Zaux != nil { allowedDirs[test.expected.Zaux.RootDirectory] = struct{}{} allowedDirs["/tmp/testdata/nginx/"] = struct{}{} } - result, err := GetNginxConfig(test.fileName, nginxID, systemID, allowedDirs) + result, err := GetNginxConfig(test.fileName, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) assert.Equal(t, test.expected.Action, result.Action) @@ -750,7 +751,8 @@ func TestGetStatusApiInfo(t *testing.T) { output := bytes.Replace(input, []byte("127.0.0.1:80"), []byte(splitUrl), -1) assert.NoError(t, os.WriteFile(test.fileName, output, 0664)) - result, err := GetStatusApiInfo(test.fileName) + ignoreDirectives := []string{} + result, err := GetStatusApiInfo(test.fileName, ignoreDirectives) //Update port in expected plusApi with the port of the mock server test.plusApi = strings.Replace(test.plusApi, ":80", ":"+strings.Split(splitUrl, ":")[1], 1) @@ -1093,8 +1095,9 @@ func TestGetErrorAndAccessLogs(t *testing.T) { err = setUpFile(test.fileName, []byte(test.config)) assert.NoError(t, err) + ignoreDirectives := []string{} - errorLogs, accessLogs, err := GetErrorAndAccessLogs(test.fileName) + errorLogs, accessLogs, err := GetErrorAndAccessLogs(test.fileName, ignoreDirectives) assert.NoError(t, err) for index, accessLog := range accessLogs.AccessLog { @@ -1649,11 +1652,12 @@ func TestGetAppProtectPolicyAndSecurityLogFiles(t *testing.T) { assert.NoError(t, err) allowedDirs := map[string]struct{}{} + ignoreDirectives := []string{} - cfg, err := GetNginxConfig(tc.file, nginxID, systemID, allowedDirs) + cfg, err := GetNginxConfig(tc.file, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) - policies, profiles := GetAppProtectPolicyAndSecurityLogFiles(cfg) + policies, profiles := GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) assert.ElementsMatch(t, tc.expPolicies, policies) assert.ElementsMatch(t, tc.expProfiles, profiles) }) diff --git a/src/core/config/config.go b/src/core/config/config.go index a5c5d5531f..32b3187aab 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -181,6 +181,7 @@ func GetConfig(clientId string) (*Config, error) { AllowedDirectoriesMap: map[string]struct{}{}, DisplayName: Viper.GetString(DisplayNameKey), InstanceGroup: Viper.GetString(InstanceGroupKey), + IgnoreDirectives: Viper.GetStringSlice(IgnoreDirectivesKey), } for _, dir := range strings.Split(config.ConfigDirs, ":") { diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 32104583dd..c125709daf 100644 --- a/src/core/config/defaults.go +++ b/src/core/config/defaults.go @@ -65,6 +65,7 @@ var ( TreatWarningsAsErrors: false, }, ConfigDirs: "/etc/nginx:/usr/local/etc/nginx:/usr/share/nginx/modules:/etc/nms", + IgnoreDirectives: []string{}, AllowedDirectoriesMap: map[string]struct{}{}, TLS: TLSConfig{ Enable: false, @@ -100,12 +101,13 @@ const ( ConfigPathKey = "path" DynamicConfigPathKey = "dynamic-config-path" - CloudAccountIdKey = "cloudaccountid" - LocationKey = "location" - DisplayNameKey = "display_name" - InstanceGroupKey = "instance_group" - ConfigDirsKey = "config_dirs" - TagsKey = "tags" + CloudAccountIdKey = "cloudaccountid" + LocationKey = "location" + DisplayNameKey = "display_name" + InstanceGroupKey = "instance_group" + ConfigDirsKey = "config_dirs" + TagsKey = "tags" + IgnoreDirectivesKey = "ignore_directives" // viper keys used in config LogKey = "log" @@ -268,6 +270,11 @@ var ( DefaultValue: agent_config.GetDefaultFeatures(), }, // NGINX Config + &StringSliceFlag{ + Name: IgnoreDirectivesKey, + Usage: "A comma-separated list of ignoring directives which contain sensitive info.", + DefaultValue: Defaults.IgnoreDirectives, + }, &StringFlag{ Name: NginxExcludeLogs, Usage: "One or more NGINX access log paths that you want to exclude from metrics collection. This key is formatted as a string and multiple values should be provided as a comma-separated list.", diff --git a/src/core/config/types.go b/src/core/config/types.go index 234a2b90cc..363fac0aa2 100644 --- a/src/core/config/types.go +++ b/src/core/config/types.go @@ -31,6 +31,7 @@ type Config struct { AllowedDirectoriesMap map[string]struct{} `yaml:"-"` DisplayName string `mapstructure:"display_name" yaml:"display_name,omitempty"` InstanceGroup string `mapstructure:"instance_group" yaml:"instance_group,omitempty"` + IgnoreDirectives []string `mapstructure:"ignore_directives" yaml:"ignore_directives,omitempty"` } func (c *Config) IsGrpcServerConfigured() bool { diff --git a/src/core/environment_test.go b/src/core/environment_test.go index 780eccdd3d..64d212002a 100644 --- a/src/core/environment_test.go +++ b/src/core/environment_test.go @@ -621,7 +621,7 @@ func TestWriteFiles(t *testing.T) { for _, file := range files { assert.NoFileExists(t, file.GetName()) } - backup, err := sdk.NewConfigApply("", nil) + backup, err := sdk.NewConfigApply("", nil, []string{}) assert.NoError(t, err) env := EnvironmentType{} @@ -655,7 +655,7 @@ func TestWriteFilesWhenExists(t *testing.T) { AllowedDirectoriesMap := map[string]struct{}{"/tmp": {}} - backup, err := sdk.NewConfigApply("", nil) + backup, err := sdk.NewConfigApply("", nil, []string{}) assert.NoError(t, err) for _, file := range files { assert.NoFileExists(t, file.GetName()) @@ -685,7 +685,7 @@ func TestWriteFilesNotAllowed(t *testing.T) { Permissions: "0644", }, } - backup, err := sdk.NewConfigApply("", nil) + backup, err := sdk.NewConfigApply("", nil, []string{}) assert.NoError(t, err) AllowedDirectoriesMap := map[string]struct{}{"/opt": {}} @@ -704,7 +704,7 @@ func TestWriteFile(t *testing.T) { Contents: []byte("contents"), Permissions: "0777", } - backup, err := sdk.NewConfigApply("", nil) + backup, err := sdk.NewConfigApply("", nil, []string{}) assert.NoError(t, err) assert.NoError(t, writeFile(backup, file, "/tmp")) assert.FileExists(t, file.GetName()) diff --git a/src/core/nginx.go b/src/core/nginx.go index b2adc428e2..713372cec2 100644 --- a/src/core/nginx.go +++ b/src/core/nginx.go @@ -203,7 +203,7 @@ func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *prot nginxDetailsFacade.ConfPath = path } - url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath) + url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) if err != nil { log.Tracef("Unable to get status api from the configuration: NGINX metrics will be unavailable for this system. please configure a status API to get NGINX metrics: %v", err) } @@ -352,6 +352,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp config.ConfigData.NginxId, config.ConfigData.SystemId, n.config.AllowedDirectoriesMap, + n.config.IgnoreDirectives, ) if err != nil { return nil, err @@ -387,7 +388,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp log.Info("Updating NGINX config") var configApply *sdk.ConfigApply - configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap) + configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return nil, err @@ -482,7 +483,7 @@ func generateDeleteFromDirectoryMap( } func (n *NginxBinaryType) ReadConfig(confFile, nginxId, systemId string) (*proto.NginxConfig, error) { - configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap) + configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { return nil, err } @@ -534,7 +535,7 @@ func (n *NginxBinaryType) writeBackup(config *proto.NginxConfig, confFiles []*pr allowedDirs := map[string]struct{}{"/tmp": {}} path := filepath.Join("/tmp", strconv.FormatInt(time.Now().Unix(), 10)) - configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap) + configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata.go b/src/extensions/nginx-app-protect/nap/nap_metadata.go index e13d805633..57d54932f5 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -24,6 +24,7 @@ import ( func UpdateMetadata( cfg *proto.NginxConfig, appProtectWAFDetails *proto.AppProtectWAFDetails, + ignoreDirectives []string, ) error { previousPrecompiledPublication := false previousMeta := Metadata{} @@ -49,7 +50,7 @@ func UpdateMetadata( return nil } - policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg) + policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) policyBundles := []*BundleMetadata{} profileBundles := []*BundleMetadata{} diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go index 7f0bbf5e6b..97a89c6b94 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go @@ -158,8 +158,9 @@ func TestUpdateNapMetadata(t *testing.T) { err := setUpFile(configFile, []byte(config)) assert.NoError(t, err) allowedDirs := map[string]struct{}{} + ignoreDirectives := []string{} - cfg, err := sdk.GetNginxConfig(configFile, nginxID, systemID, allowedDirs) + cfg, err := sdk.GetNginxConfig(configFile, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) appProtectWAFDetails := &proto.AppProtectWAFDetails{ @@ -169,8 +170,9 @@ func TestUpdateNapMetadata(t *testing.T) { WafLocation: metadataFile, PrecompiledPublication: tc.precompPub, } + ignoreDirecitves := []string{} - err = UpdateMetadata(cfg, appProtectWAFDetails) + err = UpdateMetadata(cfg, appProtectWAFDetails, ignoreDirecitves) assert.NoError(t, err) data, err := os.ReadFile(metadataFile) diff --git a/src/plugins/agent_api.go b/src/plugins/agent_api.go index 93dfefa950..6dd6c882be 100644 --- a/src/plugins/agent_api.go +++ b/src/plugins/agent_api.go @@ -484,7 +484,7 @@ func (h *NginxHandler) applyNginxConfig(nginxDetail *proto.NginxDetails, buf *by Contents: buf.Bytes(), } - configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap) + configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) if err != nil { return fmt.Errorf("unable to write config: %v", err) } diff --git a/src/plugins/metrics.go b/src/plugins/metrics.go index 75663fe737..dad221b8e2 100644 --- a/src/plugins/metrics.go +++ b/src/plugins/metrics.go @@ -300,7 +300,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina stubStatusApi = detail.StatusUrl } - errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath) + errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath, config.IgnoreDirectives) if err != nil { log.Warnf("Error reading access and error logs from config %s %v", detail.ConfPath, err) } diff --git a/src/plugins/nginx.go b/src/plugins/nginx.go index 7e38456a88..8c17d575ed 100644 --- a/src/plugins/nginx.go +++ b/src/plugins/nginx.go @@ -252,7 +252,7 @@ func (n *Nginx) uploadConfig(config *proto.ConfigDescriptor, messageId string) e } if n.isNginxAppProtectEnabled { - err = nap.UpdateMetadata(cfg, n.nginxAppProtectSoftwareDetails) + err = nap.UpdateMetadata(cfg, n.nginxAppProtectSoftwareDetails, n.config.IgnoreDirectives) if err != nil { log.Errorf("Unable to update NAP metadata: %v", err) } diff --git a/src/plugins/nginx_test.go b/src/plugins/nginx_test.go index 90ecbb42b1..5e323ebd56 100644 --- a/src/plugins/nginx_test.go +++ b/src/plugins/nginx_test.go @@ -523,8 +523,9 @@ func TestNginxConfigApply(t *testing.T) { env := tutils.GetMockEnvWithProcess() allowedDirectoriesMap := map[string]struct{}{dir: {}} + ignoreDirectives := []string{} - config, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap) + config, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) binary := tutils.NewMockNginxBinary() @@ -895,7 +896,8 @@ func TestNginx_completeConfigApply(t *testing.T) { tempConf, err := os.CreateTemp(dir, "nginx.conf") assert.NoError(t, err) allowedDirectoriesMap := map[string]struct{}{dir: {}} - configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap) + ignoreDirectives := []string{} + configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) response := &NginxConfigValidationResponse{ @@ -987,7 +989,8 @@ func TestNginx_rollbackConfigApply(t *testing.T) { tempConf, err := os.CreateTemp(dir, "nginx.conf") assert.NoError(t, err) allowedDirectoriesMap := map[string]struct{}{dir: {}} - configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap) + ignoreDirectives := []string{} + configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) response := &NginxConfigValidationResponse{ diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 00660591bd..3df4d29fb4 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -35,6 +35,7 @@ type ConfigApply struct { func NewConfigApply( confFile string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*ConfigApply, error) { w, err := zip.NewWriter("/") if err != nil { @@ -47,7 +48,7 @@ func NewConfigApply( notExistDirs: make(map[string]struct{}), } if confFile != "" { - return b, b.mapCurrentFiles(confFile, allowedDirectories) + return b, b.mapCurrentFiles(confFile, allowedDirectories, ignoreDirectives) } return b, nil } @@ -179,10 +180,11 @@ func (b *ConfigApply) RemoveFromNotExists(fullPath string) { // mapCurrentFiles parse the provided file via cross-plane, generate a list of files, which should be identical to the // DirectoryMap, will mark off the files as the config is being applied, any leftovers after complete should be deleted. -func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}) error { +func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string) error { log.Debugf("parsing %s", confFile) payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 940d401770..0635b4ebc7 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -100,9 +100,11 @@ func GetNginxConfig( nginxId, systemId string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*proto.NginxConfig, error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -735,9 +737,10 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string) (statusApi string, err error) { +func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, CombineConfigs: true, @@ -756,7 +759,7 @@ func GetStatusApiInfo(confFile string) (statusApi string, err error) { return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -770,6 +773,7 @@ func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -835,7 +839,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -845,6 +849,7 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, [ payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 00660591bd..3df4d29fb4 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -35,6 +35,7 @@ type ConfigApply struct { func NewConfigApply( confFile string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*ConfigApply, error) { w, err := zip.NewWriter("/") if err != nil { @@ -47,7 +48,7 @@ func NewConfigApply( notExistDirs: make(map[string]struct{}), } if confFile != "" { - return b, b.mapCurrentFiles(confFile, allowedDirectories) + return b, b.mapCurrentFiles(confFile, allowedDirectories, ignoreDirectives) } return b, nil } @@ -179,10 +180,11 @@ func (b *ConfigApply) RemoveFromNotExists(fullPath string) { // mapCurrentFiles parse the provided file via cross-plane, generate a list of files, which should be identical to the // DirectoryMap, will mark off the files as the config is being applied, any leftovers after complete should be deleted. -func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}) error { +func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string) error { log.Debugf("parsing %s", confFile) payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 940d401770..0635b4ebc7 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -100,9 +100,11 @@ func GetNginxConfig( nginxId, systemId string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*proto.NginxConfig, error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -735,9 +737,10 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string) (statusApi string, err error) { +func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, CombineConfigs: true, @@ -756,7 +759,7 @@ func GetStatusApiInfo(confFile string) (statusApi string, err error) { return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -770,6 +773,7 @@ func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -835,7 +839,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -845,6 +849,7 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, [ payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go index a5c5d5531f..32b3187aab 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go @@ -181,6 +181,7 @@ func GetConfig(clientId string) (*Config, error) { AllowedDirectoriesMap: map[string]struct{}{}, DisplayName: Viper.GetString(DisplayNameKey), InstanceGroup: Viper.GetString(InstanceGroupKey), + IgnoreDirectives: Viper.GetStringSlice(IgnoreDirectivesKey), } for _, dir := range strings.Split(config.ConfigDirs, ":") { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go index 32104583dd..c125709daf 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go @@ -65,6 +65,7 @@ var ( TreatWarningsAsErrors: false, }, ConfigDirs: "/etc/nginx:/usr/local/etc/nginx:/usr/share/nginx/modules:/etc/nms", + IgnoreDirectives: []string{}, AllowedDirectoriesMap: map[string]struct{}{}, TLS: TLSConfig{ Enable: false, @@ -100,12 +101,13 @@ const ( ConfigPathKey = "path" DynamicConfigPathKey = "dynamic-config-path" - CloudAccountIdKey = "cloudaccountid" - LocationKey = "location" - DisplayNameKey = "display_name" - InstanceGroupKey = "instance_group" - ConfigDirsKey = "config_dirs" - TagsKey = "tags" + CloudAccountIdKey = "cloudaccountid" + LocationKey = "location" + DisplayNameKey = "display_name" + InstanceGroupKey = "instance_group" + ConfigDirsKey = "config_dirs" + TagsKey = "tags" + IgnoreDirectivesKey = "ignore_directives" // viper keys used in config LogKey = "log" @@ -268,6 +270,11 @@ var ( DefaultValue: agent_config.GetDefaultFeatures(), }, // NGINX Config + &StringSliceFlag{ + Name: IgnoreDirectivesKey, + Usage: "A comma-separated list of ignoring directives which contain sensitive info.", + DefaultValue: Defaults.IgnoreDirectives, + }, &StringFlag{ Name: NginxExcludeLogs, Usage: "One or more NGINX access log paths that you want to exclude from metrics collection. This key is formatted as a string and multiple values should be provided as a comma-separated list.", diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go index 234a2b90cc..363fac0aa2 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/types.go @@ -31,6 +31,7 @@ type Config struct { AllowedDirectoriesMap map[string]struct{} `yaml:"-"` DisplayName string `mapstructure:"display_name" yaml:"display_name,omitempty"` InstanceGroup string `mapstructure:"instance_group" yaml:"instance_group,omitempty"` + IgnoreDirectives []string `mapstructure:"ignore_directives" yaml:"ignore_directives,omitempty"` } func (c *Config) IsGrpcServerConfigured() bool { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go index b2adc428e2..713372cec2 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go @@ -203,7 +203,7 @@ func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *prot nginxDetailsFacade.ConfPath = path } - url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath) + url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) if err != nil { log.Tracef("Unable to get status api from the configuration: NGINX metrics will be unavailable for this system. please configure a status API to get NGINX metrics: %v", err) } @@ -352,6 +352,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp config.ConfigData.NginxId, config.ConfigData.SystemId, n.config.AllowedDirectoriesMap, + n.config.IgnoreDirectives, ) if err != nil { return nil, err @@ -387,7 +388,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp log.Info("Updating NGINX config") var configApply *sdk.ConfigApply - configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap) + configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return nil, err @@ -482,7 +483,7 @@ func generateDeleteFromDirectoryMap( } func (n *NginxBinaryType) ReadConfig(confFile, nginxId, systemId string) (*proto.NginxConfig, error) { - configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap) + configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { return nil, err } @@ -534,7 +535,7 @@ func (n *NginxBinaryType) writeBackup(config *proto.NginxConfig, confFiles []*pr allowedDirs := map[string]struct{}{"/tmp": {}} path := filepath.Join("/tmp", strconv.FormatInt(time.Now().Unix(), 10)) - configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap) + configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go index e13d805633..57d54932f5 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -24,6 +24,7 @@ import ( func UpdateMetadata( cfg *proto.NginxConfig, appProtectWAFDetails *proto.AppProtectWAFDetails, + ignoreDirectives []string, ) error { previousPrecompiledPublication := false previousMeta := Metadata{} @@ -49,7 +50,7 @@ func UpdateMetadata( return nil } - policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg) + policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) policyBundles := []*BundleMetadata{} profileBundles := []*BundleMetadata{} diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go index 93dfefa950..6dd6c882be 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go @@ -484,7 +484,7 @@ func (h *NginxHandler) applyNginxConfig(nginxDetail *proto.NginxDetails, buf *by Contents: buf.Bytes(), } - configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap) + configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) if err != nil { return fmt.Errorf("unable to write config: %v", err) } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go index 75663fe737..dad221b8e2 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go @@ -300,7 +300,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina stubStatusApi = detail.StatusUrl } - errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath) + errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath, config.IgnoreDirectives) if err != nil { log.Warnf("Error reading access and error logs from config %s %v", detail.ConfPath, err) } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/nginx.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/nginx.go index 7e38456a88..8c17d575ed 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/nginx.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/nginx.go @@ -252,7 +252,7 @@ func (n *Nginx) uploadConfig(config *proto.ConfigDescriptor, messageId string) e } if n.isNginxAppProtectEnabled { - err = nap.UpdateMetadata(cfg, n.nginxAppProtectSoftwareDetails) + err = nap.UpdateMetadata(cfg, n.nginxAppProtectSoftwareDetails, n.config.IgnoreDirectives) if err != nil { log.Errorf("Unable to update NAP metadata: %v", err) } diff --git a/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 00660591bd..3df4d29fb4 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -35,6 +35,7 @@ type ConfigApply struct { func NewConfigApply( confFile string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*ConfigApply, error) { w, err := zip.NewWriter("/") if err != nil { @@ -47,7 +48,7 @@ func NewConfigApply( notExistDirs: make(map[string]struct{}), } if confFile != "" { - return b, b.mapCurrentFiles(confFile, allowedDirectories) + return b, b.mapCurrentFiles(confFile, allowedDirectories, ignoreDirectives) } return b, nil } @@ -179,10 +180,11 @@ func (b *ConfigApply) RemoveFromNotExists(fullPath string) { // mapCurrentFiles parse the provided file via cross-plane, generate a list of files, which should be identical to the // DirectoryMap, will mark off the files as the config is being applied, any leftovers after complete should be deleted. -func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}) error { +func (b *ConfigApply) mapCurrentFiles(confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string) error { log.Debugf("parsing %s", confFile) payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, diff --git a/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 940d401770..0635b4ebc7 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -100,9 +100,11 @@ func GetNginxConfig( nginxId, systemId string, allowedDirectories map[string]struct{}, + ignoreDirectives []string, ) (*proto.NginxConfig, error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -735,9 +737,10 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string) (statusApi string, err error) { +func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, CombineConfigs: true, @@ -756,7 +759,7 @@ func GetStatusApiInfo(confFile string) (statusApi string, err error) { return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -770,6 +773,7 @@ func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, @@ -835,7 +839,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -845,6 +849,7 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, [ payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ + IgnoreDirectives: ignoreDirectives, SingleFile: false, StopParsingOnError: true, }, From 8f957b34993b6a54ca41e3797b96cb4bf276d350 Mon Sep 17 00:00:00 2001 From: aphralG <108004222+aphralG@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:55:51 +0100 Subject: [PATCH 2/4] fix performance tests (#358) * fix performance tests --- sdk/config_apply.go | 10 +++++- sdk/config_apply_test.go | 4 +-- sdk/config_helpers.go | 35 ++++++++++++++++--- sdk/config_helpers_test.go | 10 +++--- src/core/environment_test.go | 8 ++--- src/core/nginx.go | 10 +++--- .../nginx-app-protect/nap/nap_metadata.go | 2 +- .../nap/nap_metadata_test.go | 2 +- src/plugins/agent_api.go | 2 +- src/plugins/metrics.go | 2 +- src/plugins/nginx_test.go | 6 ++-- .../nginx/agent/sdk/v2/config_apply.go | 10 +++++- .../nginx/agent/sdk/v2/config_helpers.go | 35 ++++++++++++++++--- .../nginx/agent/sdk/v2/config_apply.go | 10 +++++- .../nginx/agent/sdk/v2/config_helpers.go | 35 ++++++++++++++++--- .../nginx/agent/v2/src/core/nginx.go | 10 +++--- .../nginx-app-protect/nap/nap_metadata.go | 2 +- .../nginx/agent/v2/src/plugins/agent_api.go | 2 +- .../nginx/agent/v2/src/plugins/metrics.go | 2 +- .../nginx/agent/sdk/v2/config_apply.go | 10 +++++- .../nginx/agent/sdk/v2/config_helpers.go | 35 ++++++++++++++++--- 21 files changed, 187 insertions(+), 55 deletions(-) diff --git a/sdk/config_apply.go b/sdk/config_apply.go index 3df4d29fb4..dbfbcfa1b9 100644 --- a/sdk/config_apply.go +++ b/sdk/config_apply.go @@ -32,7 +32,7 @@ type ConfigApply struct { notExistDirs map[string]struct{} // set of directories that exists in the config provided payload, but not on disk } -func NewConfigApply( +func NewConfigApplyWithIgnoreDirectives( confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string, @@ -53,6 +53,14 @@ func NewConfigApply( return b, nil } +// to ignore directives use NewConfigApplyWithIgnoreDirectives() +func NewConfigApply( + confFile string, + allowedDirectories map[string]struct{}, +) (*ConfigApply, error) { + return NewConfigApplyWithIgnoreDirectives(confFile, allowedDirectories, []string{}) +} + // Rollback dumps the saved file content, and delete the notExists file. Best effort, will log error and continue // if file operation failed during rollback. func (b *ConfigApply) Rollback(cause error) error { diff --git a/sdk/config_apply_test.go b/sdk/config_apply_test.go index 0ec126ba03..f89a31e13c 100644 --- a/sdk/config_apply_test.go +++ b/sdk/config_apply_test.go @@ -162,7 +162,7 @@ func TestNewConfigApply(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - configApply, err := NewConfigApply(tc.confFile, tc.allowedDirectories, tc.ignoreDirectives) + configApply, err := NewConfigApplyWithIgnoreDirectives(tc.confFile, tc.allowedDirectories, tc.ignoreDirectives) assert.Equal(t, tc.expectedConfigApply.existing, configApply.GetExisting()) assert.Equal(t, tc.expectedConfigApply.notExists, configApply.GetNotExists()) assert.Equal(t, tc.expectedConfigApply.notExistDirs, configApply.GetNotExistDirs()) @@ -273,7 +273,7 @@ func TestConfigApplyCompleteAndRollback(t *testing.T) { allowedDirectories := map[string]struct{}{tmpDir: {}} ignoreDirectives := []string{} - configApply, err := NewConfigApply(confFile, allowedDirectories, ignoreDirectives) + configApply, err := NewConfigApplyWithIgnoreDirectives(confFile, allowedDirectories, ignoreDirectives) assert.Equal(t, 5, len(configApply.GetExisting())) assert.Nil(t, err) diff --git a/sdk/config_helpers.go b/sdk/config_helpers.go index 0635b4ebc7..620c58b2e6 100644 --- a/sdk/config_helpers.go +++ b/sdk/config_helpers.go @@ -92,10 +92,10 @@ func (dm DirectoryMap) appendFileWithProto(dir string, fileProto *proto.File) er return nil } -// GetNginxConfig parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for +// GetNginxConfigWithIgnoreDirectives parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for // ConfigDescriptor in the NginxConfig. The allowedDirectories is used to allowlist the directories we include // in the aux payload. -func GetNginxConfig( +func GetNginxConfigWithIgnoreDirectives( confFile, nginxId, systemId string, @@ -135,6 +135,16 @@ func GetNginxConfig( return nginxConfig, nil } +// to ignore directives use GetNginxConfigWithIgnoreDirectives() +func GetNginxConfig( + confFile, + nginxId, + systemId string, + allowedDirectories map[string]struct{}, +) (*proto.NginxConfig, error) { + return GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, allowedDirectories, []string{}) +} + // updateNginxConfigFromPayload updates config files from payload. func updateNginxConfigFromPayload( confFile string, @@ -737,7 +747,7 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { +func GetStatusApiInfoWithIgnoreDirectives(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ IgnoreDirectives: ignoreDirectives, @@ -759,7 +769,12 @@ func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi str return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +// to ignore directives use GetStatusApiInfoWithIgnoreDirectives() +func GetStatusApiInfo(confFile string) (statusApi string, err error) { + return GetStatusApiInfoWithIgnoreDirectives(confFile, []string{}) +} + +func GetErrorAndAccessLogsWithIgnoreDirectives(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -800,6 +815,11 @@ func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.E return nginxConfig.ErrorLogs, nginxConfig.AccessLogs, err } +// to ignore directives use GetErrorAndAccessLogsWithIgnoreDirectives() +func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { + return GetErrorAndAccessLogsWithIgnoreDirectives(confFile, []string{}) +} + func GetErrorLogs(errorLogs *proto.ErrorLogs) []string { result := []string{} for _, log := range errorLogs.ErrorLog { @@ -839,7 +859,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -897,3 +917,8 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirect return policies, profiles } + +// to ignore directives use GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives() +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { + return GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, []string{}) +} diff --git a/sdk/config_helpers_test.go b/sdk/config_helpers_test.go index 2d23305701..a82140514f 100644 --- a/sdk/config_helpers_test.go +++ b/sdk/config_helpers_test.go @@ -639,7 +639,7 @@ func TestGetNginxConfig(t *testing.T) { allowedDirs[test.expected.Zaux.RootDirectory] = struct{}{} allowedDirs["/tmp/testdata/nginx/"] = struct{}{} } - result, err := GetNginxConfig(test.fileName, nginxID, systemID, allowedDirs, ignoreDirectives) + result, err := GetNginxConfigWithIgnoreDirectives(test.fileName, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) assert.Equal(t, test.expected.Action, result.Action) @@ -752,7 +752,7 @@ func TestGetStatusApiInfo(t *testing.T) { assert.NoError(t, os.WriteFile(test.fileName, output, 0664)) ignoreDirectives := []string{} - result, err := GetStatusApiInfo(test.fileName, ignoreDirectives) + result, err := GetStatusApiInfoWithIgnoreDirectives(test.fileName, ignoreDirectives) //Update port in expected plusApi with the port of the mock server test.plusApi = strings.Replace(test.plusApi, ":80", ":"+strings.Split(splitUrl, ":")[1], 1) @@ -1097,7 +1097,7 @@ func TestGetErrorAndAccessLogs(t *testing.T) { assert.NoError(t, err) ignoreDirectives := []string{} - errorLogs, accessLogs, err := GetErrorAndAccessLogs(test.fileName, ignoreDirectives) + errorLogs, accessLogs, err := GetErrorAndAccessLogsWithIgnoreDirectives(test.fileName, ignoreDirectives) assert.NoError(t, err) for index, accessLog := range accessLogs.AccessLog { @@ -1654,10 +1654,10 @@ func TestGetAppProtectPolicyAndSecurityLogFiles(t *testing.T) { allowedDirs := map[string]struct{}{} ignoreDirectives := []string{} - cfg, err := GetNginxConfig(tc.file, nginxID, systemID, allowedDirs, ignoreDirectives) + cfg, err := GetNginxConfigWithIgnoreDirectives(tc.file, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) - policies, profiles := GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) + policies, profiles := GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, ignoreDirectives) assert.ElementsMatch(t, tc.expPolicies, policies) assert.ElementsMatch(t, tc.expProfiles, profiles) }) diff --git a/src/core/environment_test.go b/src/core/environment_test.go index 64d212002a..701a51d560 100644 --- a/src/core/environment_test.go +++ b/src/core/environment_test.go @@ -621,7 +621,7 @@ func TestWriteFiles(t *testing.T) { for _, file := range files { assert.NoFileExists(t, file.GetName()) } - backup, err := sdk.NewConfigApply("", nil, []string{}) + backup, err := sdk.NewConfigApplyWithIgnoreDirectives("", nil, []string{}) assert.NoError(t, err) env := EnvironmentType{} @@ -655,7 +655,7 @@ func TestWriteFilesWhenExists(t *testing.T) { AllowedDirectoriesMap := map[string]struct{}{"/tmp": {}} - backup, err := sdk.NewConfigApply("", nil, []string{}) + backup, err := sdk.NewConfigApplyWithIgnoreDirectives("", nil, []string{}) assert.NoError(t, err) for _, file := range files { assert.NoFileExists(t, file.GetName()) @@ -685,7 +685,7 @@ func TestWriteFilesNotAllowed(t *testing.T) { Permissions: "0644", }, } - backup, err := sdk.NewConfigApply("", nil, []string{}) + backup, err := sdk.NewConfigApplyWithIgnoreDirectives("", nil, []string{}) assert.NoError(t, err) AllowedDirectoriesMap := map[string]struct{}{"/opt": {}} @@ -704,7 +704,7 @@ func TestWriteFile(t *testing.T) { Contents: []byte("contents"), Permissions: "0777", } - backup, err := sdk.NewConfigApply("", nil, []string{}) + backup, err := sdk.NewConfigApplyWithIgnoreDirectives("", nil, []string{}) assert.NoError(t, err) assert.NoError(t, writeFile(backup, file, "/tmp")) assert.FileExists(t, file.GetName()) diff --git a/src/core/nginx.go b/src/core/nginx.go index 713372cec2..537357c893 100644 --- a/src/core/nginx.go +++ b/src/core/nginx.go @@ -203,7 +203,7 @@ func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *prot nginxDetailsFacade.ConfPath = path } - url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) + url, err := sdk.GetStatusApiInfoWithIgnoreDirectives(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) if err != nil { log.Tracef("Unable to get status api from the configuration: NGINX metrics will be unavailable for this system. please configure a status API to get NGINX metrics: %v", err) } @@ -347,7 +347,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp return nil, fmt.Errorf("NGINX instance %s not found", config.ConfigData.NginxId) } - systemNginxConfig, err := sdk.GetNginxConfig( + systemNginxConfig, err := sdk.GetNginxConfigWithIgnoreDirectives( details.ConfPath, config.ConfigData.NginxId, config.ConfigData.SystemId, @@ -388,7 +388,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp log.Info("Updating NGINX config") var configApply *sdk.ConfigApply - configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configApply, err = sdk.NewConfigApplyWithIgnoreDirectives(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return nil, err @@ -483,7 +483,7 @@ func generateDeleteFromDirectoryMap( } func (n *NginxBinaryType) ReadConfig(confFile, nginxId, systemId string) (*proto.NginxConfig, error) { - configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configPayload, err := sdk.GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { return nil, err } @@ -535,7 +535,7 @@ func (n *NginxBinaryType) writeBackup(config *proto.NginxConfig, confFiles []*pr allowedDirs := map[string]struct{}{"/tmp": {}} path := filepath.Join("/tmp", strconv.FormatInt(time.Now().Unix(), 10)) - configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata.go b/src/extensions/nginx-app-protect/nap/nap_metadata.go index 57d54932f5..d19d1f4129 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -50,7 +50,7 @@ func UpdateMetadata( return nil } - policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) + policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, ignoreDirectives) policyBundles := []*BundleMetadata{} profileBundles := []*BundleMetadata{} diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go index 97a89c6b94..3a068bd578 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go @@ -160,7 +160,7 @@ func TestUpdateNapMetadata(t *testing.T) { allowedDirs := map[string]struct{}{} ignoreDirectives := []string{} - cfg, err := sdk.GetNginxConfig(configFile, nginxID, systemID, allowedDirs, ignoreDirectives) + cfg, err := sdk.GetNginxConfigWithIgnoreDirectives(configFile, nginxID, systemID, allowedDirs, ignoreDirectives) assert.NoError(t, err) appProtectWAFDetails := &proto.AppProtectWAFDetails{ diff --git a/src/plugins/agent_api.go b/src/plugins/agent_api.go index 6dd6c882be..e819ea72f9 100644 --- a/src/plugins/agent_api.go +++ b/src/plugins/agent_api.go @@ -484,7 +484,7 @@ func (h *NginxHandler) applyNginxConfig(nginxDetail *proto.NginxDetails, buf *by Contents: buf.Bytes(), } - configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) if err != nil { return fmt.Errorf("unable to write config: %v", err) } diff --git a/src/plugins/metrics.go b/src/plugins/metrics.go index dad221b8e2..d52588046d 100644 --- a/src/plugins/metrics.go +++ b/src/plugins/metrics.go @@ -300,7 +300,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina stubStatusApi = detail.StatusUrl } - errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath, config.IgnoreDirectives) + errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogsWithIgnoreDirectives(detail.ConfPath, config.IgnoreDirectives) if err != nil { log.Warnf("Error reading access and error logs from config %s %v", detail.ConfPath, err) } diff --git a/src/plugins/nginx_test.go b/src/plugins/nginx_test.go index 5e323ebd56..9155205516 100644 --- a/src/plugins/nginx_test.go +++ b/src/plugins/nginx_test.go @@ -525,7 +525,7 @@ func TestNginxConfigApply(t *testing.T) { allowedDirectoriesMap := map[string]struct{}{dir: {}} ignoreDirectives := []string{} - config, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) + config, err := sdk.NewConfigApplyWithIgnoreDirectives(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) binary := tutils.NewMockNginxBinary() @@ -897,7 +897,7 @@ func TestNginx_completeConfigApply(t *testing.T) { assert.NoError(t, err) allowedDirectoriesMap := map[string]struct{}{dir: {}} ignoreDirectives := []string{} - configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) response := &NginxConfigValidationResponse{ @@ -990,7 +990,7 @@ func TestNginx_rollbackConfigApply(t *testing.T) { assert.NoError(t, err) allowedDirectoriesMap := map[string]struct{}{dir: {}} ignoreDirectives := []string{} - configApply, err := sdk.NewConfigApply(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives(tempConf.Name(), allowedDirectoriesMap, ignoreDirectives) assert.NoError(t, err) response := &NginxConfigValidationResponse{ diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 3df4d29fb4..dbfbcfa1b9 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -32,7 +32,7 @@ type ConfigApply struct { notExistDirs map[string]struct{} // set of directories that exists in the config provided payload, but not on disk } -func NewConfigApply( +func NewConfigApplyWithIgnoreDirectives( confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string, @@ -53,6 +53,14 @@ func NewConfigApply( return b, nil } +// to ignore directives use NewConfigApplyWithIgnoreDirectives() +func NewConfigApply( + confFile string, + allowedDirectories map[string]struct{}, +) (*ConfigApply, error) { + return NewConfigApplyWithIgnoreDirectives(confFile, allowedDirectories, []string{}) +} + // Rollback dumps the saved file content, and delete the notExists file. Best effort, will log error and continue // if file operation failed during rollback. func (b *ConfigApply) Rollback(cause error) error { diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 0635b4ebc7..620c58b2e6 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -92,10 +92,10 @@ func (dm DirectoryMap) appendFileWithProto(dir string, fileProto *proto.File) er return nil } -// GetNginxConfig parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for +// GetNginxConfigWithIgnoreDirectives parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for // ConfigDescriptor in the NginxConfig. The allowedDirectories is used to allowlist the directories we include // in the aux payload. -func GetNginxConfig( +func GetNginxConfigWithIgnoreDirectives( confFile, nginxId, systemId string, @@ -135,6 +135,16 @@ func GetNginxConfig( return nginxConfig, nil } +// to ignore directives use GetNginxConfigWithIgnoreDirectives() +func GetNginxConfig( + confFile, + nginxId, + systemId string, + allowedDirectories map[string]struct{}, +) (*proto.NginxConfig, error) { + return GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, allowedDirectories, []string{}) +} + // updateNginxConfigFromPayload updates config files from payload. func updateNginxConfigFromPayload( confFile string, @@ -737,7 +747,7 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { +func GetStatusApiInfoWithIgnoreDirectives(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ IgnoreDirectives: ignoreDirectives, @@ -759,7 +769,12 @@ func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi str return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +// to ignore directives use GetStatusApiInfoWithIgnoreDirectives() +func GetStatusApiInfo(confFile string) (statusApi string, err error) { + return GetStatusApiInfoWithIgnoreDirectives(confFile, []string{}) +} + +func GetErrorAndAccessLogsWithIgnoreDirectives(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -800,6 +815,11 @@ func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.E return nginxConfig.ErrorLogs, nginxConfig.AccessLogs, err } +// to ignore directives use GetErrorAndAccessLogsWithIgnoreDirectives() +func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { + return GetErrorAndAccessLogsWithIgnoreDirectives(confFile, []string{}) +} + func GetErrorLogs(errorLogs *proto.ErrorLogs) []string { result := []string{} for _, log := range errorLogs.ErrorLog { @@ -839,7 +859,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -897,3 +917,8 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirect return policies, profiles } + +// to ignore directives use GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives() +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { + return GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, []string{}) +} diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 3df4d29fb4..dbfbcfa1b9 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -32,7 +32,7 @@ type ConfigApply struct { notExistDirs map[string]struct{} // set of directories that exists in the config provided payload, but not on disk } -func NewConfigApply( +func NewConfigApplyWithIgnoreDirectives( confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string, @@ -53,6 +53,14 @@ func NewConfigApply( return b, nil } +// to ignore directives use NewConfigApplyWithIgnoreDirectives() +func NewConfigApply( + confFile string, + allowedDirectories map[string]struct{}, +) (*ConfigApply, error) { + return NewConfigApplyWithIgnoreDirectives(confFile, allowedDirectories, []string{}) +} + // Rollback dumps the saved file content, and delete the notExists file. Best effort, will log error and continue // if file operation failed during rollback. func (b *ConfigApply) Rollback(cause error) error { diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 0635b4ebc7..620c58b2e6 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -92,10 +92,10 @@ func (dm DirectoryMap) appendFileWithProto(dir string, fileProto *proto.File) er return nil } -// GetNginxConfig parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for +// GetNginxConfigWithIgnoreDirectives parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for // ConfigDescriptor in the NginxConfig. The allowedDirectories is used to allowlist the directories we include // in the aux payload. -func GetNginxConfig( +func GetNginxConfigWithIgnoreDirectives( confFile, nginxId, systemId string, @@ -135,6 +135,16 @@ func GetNginxConfig( return nginxConfig, nil } +// to ignore directives use GetNginxConfigWithIgnoreDirectives() +func GetNginxConfig( + confFile, + nginxId, + systemId string, + allowedDirectories map[string]struct{}, +) (*proto.NginxConfig, error) { + return GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, allowedDirectories, []string{}) +} + // updateNginxConfigFromPayload updates config files from payload. func updateNginxConfigFromPayload( confFile string, @@ -737,7 +747,7 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { +func GetStatusApiInfoWithIgnoreDirectives(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ IgnoreDirectives: ignoreDirectives, @@ -759,7 +769,12 @@ func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi str return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +// to ignore directives use GetStatusApiInfoWithIgnoreDirectives() +func GetStatusApiInfo(confFile string) (statusApi string, err error) { + return GetStatusApiInfoWithIgnoreDirectives(confFile, []string{}) +} + +func GetErrorAndAccessLogsWithIgnoreDirectives(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -800,6 +815,11 @@ func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.E return nginxConfig.ErrorLogs, nginxConfig.AccessLogs, err } +// to ignore directives use GetErrorAndAccessLogsWithIgnoreDirectives() +func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { + return GetErrorAndAccessLogsWithIgnoreDirectives(confFile, []string{}) +} + func GetErrorLogs(errorLogs *proto.ErrorLogs) []string { result := []string{} for _, log := range errorLogs.ErrorLog { @@ -839,7 +859,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -897,3 +917,8 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirect return policies, profiles } + +// to ignore directives use GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives() +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { + return GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, []string{}) +} diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go index 713372cec2..537357c893 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/nginx.go @@ -203,7 +203,7 @@ func (n *NginxBinaryType) GetNginxDetailsFromProcess(nginxProcess Process) *prot nginxDetailsFacade.ConfPath = path } - url, err := sdk.GetStatusApiInfo(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) + url, err := sdk.GetStatusApiInfoWithIgnoreDirectives(nginxDetailsFacade.ConfPath, n.config.IgnoreDirectives) if err != nil { log.Tracef("Unable to get status api from the configuration: NGINX metrics will be unavailable for this system. please configure a status API to get NGINX metrics: %v", err) } @@ -347,7 +347,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp return nil, fmt.Errorf("NGINX instance %s not found", config.ConfigData.NginxId) } - systemNginxConfig, err := sdk.GetNginxConfig( + systemNginxConfig, err := sdk.GetNginxConfigWithIgnoreDirectives( details.ConfPath, config.ConfigData.NginxId, config.ConfigData.SystemId, @@ -388,7 +388,7 @@ func (n *NginxBinaryType) WriteConfig(config *proto.NginxConfig) (*sdk.ConfigApp log.Info("Updating NGINX config") var configApply *sdk.ConfigApply - configApply, err = sdk.NewConfigApply(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configApply, err = sdk.NewConfigApplyWithIgnoreDirectives(details.ConfPath, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return nil, err @@ -483,7 +483,7 @@ func generateDeleteFromDirectoryMap( } func (n *NginxBinaryType) ReadConfig(confFile, nginxId, systemId string) (*proto.NginxConfig, error) { - configPayload, err := sdk.GetNginxConfig(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configPayload, err := sdk.GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { return nil, err } @@ -535,7 +535,7 @@ func (n *NginxBinaryType) writeBackup(config *proto.NginxConfig, confFiles []*pr allowedDirs := map[string]struct{}{"/tmp": {}} path := filepath.Join("/tmp", strconv.FormatInt(time.Now().Unix(), 10)) - configApply, err := sdk.NewConfigApply("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives("/tmp", n.config.AllowedDirectoriesMap, n.config.IgnoreDirectives) if err != nil { log.Warnf("config_apply error: %s", err) return diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go index 57d54932f5..d19d1f4129 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -50,7 +50,7 @@ func UpdateMetadata( return nil } - policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFiles(cfg, ignoreDirectives) + policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, ignoreDirectives) policyBundles := []*BundleMetadata{} profileBundles := []*BundleMetadata{} diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go index 6dd6c882be..e819ea72f9 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go @@ -484,7 +484,7 @@ func (h *NginxHandler) applyNginxConfig(nginxDetail *proto.NginxDetails, buf *by Contents: buf.Bytes(), } - configApply, err := sdk.NewConfigApply(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) + configApply, err := sdk.NewConfigApplyWithIgnoreDirectives(protoFile.GetName(), h.config.AllowedDirectoriesMap, h.config.IgnoreDirectives) if err != nil { return fmt.Errorf("unable to write config: %v", err) } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go index dad221b8e2..d52588046d 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/metrics.go @@ -300,7 +300,7 @@ func createCollectorConfigsMap(config *config.Config, env core.Environment, bina stubStatusApi = detail.StatusUrl } - errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogs(detail.ConfPath, config.IgnoreDirectives) + errorLogs, accessLogs, err := sdk.GetErrorAndAccessLogsWithIgnoreDirectives(detail.ConfPath, config.IgnoreDirectives) if err != nil { log.Warnf("Error reading access and error logs from config %s %v", detail.ConfPath, err) } diff --git a/vendor/github.com/nginx/agent/sdk/v2/config_apply.go b/vendor/github.com/nginx/agent/sdk/v2/config_apply.go index 3df4d29fb4..dbfbcfa1b9 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/config_apply.go +++ b/vendor/github.com/nginx/agent/sdk/v2/config_apply.go @@ -32,7 +32,7 @@ type ConfigApply struct { notExistDirs map[string]struct{} // set of directories that exists in the config provided payload, but not on disk } -func NewConfigApply( +func NewConfigApplyWithIgnoreDirectives( confFile string, allowedDirectories map[string]struct{}, ignoreDirectives []string, @@ -53,6 +53,14 @@ func NewConfigApply( return b, nil } +// to ignore directives use NewConfigApplyWithIgnoreDirectives() +func NewConfigApply( + confFile string, + allowedDirectories map[string]struct{}, +) (*ConfigApply, error) { + return NewConfigApplyWithIgnoreDirectives(confFile, allowedDirectories, []string{}) +} + // Rollback dumps the saved file content, and delete the notExists file. Best effort, will log error and continue // if file operation failed during rollback. func (b *ConfigApply) Rollback(cause error) error { diff --git a/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go b/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go index 0635b4ebc7..620c58b2e6 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go +++ b/vendor/github.com/nginx/agent/sdk/v2/config_helpers.go @@ -92,10 +92,10 @@ func (dm DirectoryMap) appendFileWithProto(dir string, fileProto *proto.File) er return nil } -// GetNginxConfig parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for +// GetNginxConfigWithIgnoreDirectives parse the configFile into proto.NginxConfig payload, using the provided nginxID, and systemID for // ConfigDescriptor in the NginxConfig. The allowedDirectories is used to allowlist the directories we include // in the aux payload. -func GetNginxConfig( +func GetNginxConfigWithIgnoreDirectives( confFile, nginxId, systemId string, @@ -135,6 +135,16 @@ func GetNginxConfig( return nginxConfig, nil } +// to ignore directives use GetNginxConfigWithIgnoreDirectives() +func GetNginxConfig( + confFile, + nginxId, + systemId string, + allowedDirectories map[string]struct{}, +) (*proto.NginxConfig, error) { + return GetNginxConfigWithIgnoreDirectives(confFile, nginxId, systemId, allowedDirectories, []string{}) +} + // updateNginxConfigFromPayload updates config files from payload. func updateNginxConfigFromPayload( confFile string, @@ -737,7 +747,7 @@ func pingStatusAPIEndpoint(statusAPI string) bool { return true } -func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi string, err error) { +func GetStatusApiInfoWithIgnoreDirectives(confFile string, ignoreDirectives []string) (statusApi string, err error) { payload, err := crossplane.Parse(confFile, &crossplane.ParseOptions{ IgnoreDirectives: ignoreDirectives, @@ -759,7 +769,12 @@ func GetStatusApiInfo(confFile string, ignoreDirectives []string) (statusApi str return "", errors.New("no status api reachable from the agent found") } -func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { +// to ignore directives use GetStatusApiInfoWithIgnoreDirectives() +func GetStatusApiInfo(confFile string) (statusApi string, err error) { + return GetStatusApiInfoWithIgnoreDirectives(confFile, []string{}) +} + +func GetErrorAndAccessLogsWithIgnoreDirectives(confFile string, ignoreDirectives []string) (*proto.ErrorLogs, *proto.AccessLogs, error) { nginxConfig := &proto.NginxConfig{ Action: proto.NginxConfigAction_RETURN, ConfigData: nil, @@ -800,6 +815,11 @@ func GetErrorAndAccessLogs(confFile string, ignoreDirectives []string) (*proto.E return nginxConfig.ErrorLogs, nginxConfig.AccessLogs, err } +// to ignore directives use GetErrorAndAccessLogsWithIgnoreDirectives() +func GetErrorAndAccessLogs(confFile string) (*proto.ErrorLogs, *proto.AccessLogs, error) { + return GetErrorAndAccessLogsWithIgnoreDirectives(confFile, []string{}) +} + func GetErrorLogs(errorLogs *proto.ErrorLogs) []string { result := []string{} for _, log := range errorLogs.ErrorLog { @@ -839,7 +859,7 @@ func convertToHexFormat(hexString string) string { return formatted } -func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { +func GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg *proto.NginxConfig, ignoreDirectives []string) ([]string, []string) { policyMap := make(map[string]bool) profileMap := make(map[string]bool) @@ -897,3 +917,8 @@ func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig, ignoreDirect return policies, profiles } + +// to ignore directives use GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives() +func GetAppProtectPolicyAndSecurityLogFiles(cfg *proto.NginxConfig) ([]string, []string) { + return GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, []string{}) +} From 37bcea822a935cfd8db3e87cbd707ecdcc91ed20 Mon Sep 17 00:00:00 2001 From: Oliver O'Mahony Date: Thu, 29 Jun 2023 10:41:41 +0100 Subject: [PATCH 3/4] fixed tooling to pull from vendor and go.mod (#359) * fixed tooling to pull from vendor and go.mod and removed unused tool enumer --- Makefile | 6 +- go.mod | 2 - go.sum | 6 - scripts/tools.go | 15 +- sdk/client/client.go | 2 - .../nginx/agent/sdk/v2/client/client.go | 2 - .../nginx/agent/sdk/v2/client/client.go | 2 - .../github.com/alvaroloes/enumer/.gitignore | 28 - vendor/github.com/alvaroloes/enumer/LICENSE | 26 - vendor/github.com/alvaroloes/enumer/README.md | 147 ---- vendor/github.com/alvaroloes/enumer/enumer.go | 157 ---- vendor/github.com/alvaroloes/enumer/sql.go | 40 - .../github.com/alvaroloes/enumer/stringer.go | 738 ------------------ .../nginx/agent/sdk/v2/client/client.go | 2 - .../github.com/pascaldekloe/name/.travis.yml | 1 - vendor/github.com/pascaldekloe/name/LICENSE | 5 - vendor/github.com/pascaldekloe/name/README.md | 51 -- vendor/github.com/pascaldekloe/name/case.go | 129 --- vendor/modules.txt | 6 - 19 files changed, 10 insertions(+), 1355 deletions(-) delete mode 100644 vendor/github.com/alvaroloes/enumer/.gitignore delete mode 100644 vendor/github.com/alvaroloes/enumer/LICENSE delete mode 100644 vendor/github.com/alvaroloes/enumer/README.md delete mode 100644 vendor/github.com/alvaroloes/enumer/enumer.go delete mode 100644 vendor/github.com/alvaroloes/enumer/sql.go delete mode 100644 vendor/github.com/alvaroloes/enumer/stringer.go delete mode 100644 vendor/github.com/pascaldekloe/name/.travis.yml delete mode 100644 vendor/github.com/pascaldekloe/name/LICENSE delete mode 100644 vendor/github.com/pascaldekloe/name/README.md delete mode 100644 vendor/github.com/pascaldekloe/name/case.go diff --git a/Makefile b/Makefile index f41ba9e748..76d5014885 100644 --- a/Makefile +++ b/Makefile @@ -118,11 +118,9 @@ format: ## Format code go fmt ./... && cd sdk && go fmt ./... && cd ../test/performance && go fmt ./... && cd ../../test/integration && go fmt ./... buf format -w ./sdk/proto/ -install-tools: ## Install dependencies in tools.go - @echo "Getting Tools" - @grep _ ./scripts/tools.go | awk '{print $$2}' | xargs -tI % go get % +install-tools: ## Install dependencies in tools.go using vendored version see https://www.jvt.me/posts/2023/06/19/go-install-from-mod/ @echo "Installing Tools" - @grep _ ./scripts/tools.go | awk '{print $$2}' | xargs -tI % go install % + @grep _ ./scripts/tools.go | awk '{print $$2}' | xargs -tI % env GOBIN=$$(git rev-parse --show-toplevel)/bin GOWORK=off go install -mod=vendor % @go run github.com/evilmartians/lefthook install pre-push generate-swagger: ## Generates swagger.json from source code diff --git a/go.mod b/go.mod index 11155376ba..e4ec5099ed 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/nginx/agent/v2 go 1.19 require ( - github.com/alvaroloes/enumer v1.1.2 github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/fsnotify/fsnotify v1.6.0 github.com/gogo/protobuf v1.3.2 @@ -237,7 +236,6 @@ require ( github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc3 // indirect - github.com/pascaldekloe/name v1.0.1 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index 0b5b3b1120..ae30257ebe 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,6 @@ github.com/alexkohler/prealloc v1.0.0 h1:Hbq0/3fJPQhNkN0dR95AVrr6R7tou91y0uHG5pO github.com/alexkohler/prealloc v1.0.0/go.mod h1:VetnK3dIgFBBKmg0YnD9F9x6Icjd+9cvfHR56wJVlKE= github.com/alingse/asasalint v0.0.11 h1:SFwnQXJ49Kx/1GghOFz1XGqHYKp21Kq1nHad/0WQRnw= github.com/alingse/asasalint v0.0.11/go.mod h1:nCaoMhw7a9kSJObvQyVzNTPBDbNpdocqrSP7t/cW5+I= -github.com/alvaroloes/enumer v1.1.2 h1:5khqHB33TZy1GWCO/lZwcroBFh7u+0j40T83VUbfAMY= -github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -697,9 +695,6 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= -github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ= -github.com/pascaldekloe/name v1.0.1 h1:9lnXOHeqeHHnWLbKfH6X98+4+ETVqFqxN09UXSjcMb0= -github.com/pascaldekloe/name v1.0.1/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= @@ -1218,7 +1213,6 @@ golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= diff --git a/scripts/tools.go b/scripts/tools.go index 3df4fabba2..d8c23f8294 100644 --- a/scripts/tools.go +++ b/scripts/tools.go @@ -1,20 +1,21 @@ //go:build tools // +build tools +// https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/ + package tools import ( - _ "github.com/alvaroloes/enumer" + _ "github.com/bufbuild/buf/cmd/buf" + _ "github.com/evilmartians/lefthook" + _ "github.com/go-swagger/go-swagger/cmd/swagger" _ "github.com/gogo/protobuf/protoc-gen-gogo" _ "github.com/gogo/protobuf/protoc-gen-gogofast" _ "github.com/golang/mock/mockgen" + _ "github.com/golangci/golangci-lint/cmd/golangci-lint" + _ "github.com/goreleaser/nfpm/v2/cmd/nfpm" _ "github.com/maxbrunsfeld/counterfeiter/v6" _ "github.com/mwitkow/go-proto-validators/protoc-gen-govalidators" - _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" _ "github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc" - _ "github.com/go-swagger/go-swagger/cmd/swagger" - _ "github.com/bufbuild/buf/cmd/buf" - _ "github.com/goreleaser/nfpm/v2/cmd/nfpm" - _ "github.com/evilmartians/lefthook" - _ "github.com/golangci/golangci-lint/cmd/golangci-lint" + _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" ) diff --git a/sdk/client/client.go b/sdk/client/client.go index 6c79e13098..a0d133ea1f 100644 --- a/sdk/client/client.go +++ b/sdk/client/client.go @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -//go:generate enumer -type=MsgClassification -text -yaml -json -transform=snake -trimprefix=MsgClassification - package client import ( diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/client/client.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/client/client.go index 6c79e13098..a0d133ea1f 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/client/client.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/client/client.go @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -//go:generate enumer -type=MsgClassification -text -yaml -json -transform=snake -trimprefix=MsgClassification - package client import ( diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/client/client.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/client/client.go index 6c79e13098..a0d133ea1f 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/client/client.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/client/client.go @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -//go:generate enumer -type=MsgClassification -text -yaml -json -transform=snake -trimprefix=MsgClassification - package client import ( diff --git a/vendor/github.com/alvaroloes/enumer/.gitignore b/vendor/github.com/alvaroloes/enumer/.gitignore deleted file mode 100644 index 99f7832d45..0000000000 --- a/vendor/github.com/alvaroloes/enumer/.gitignore +++ /dev/null @@ -1,28 +0,0 @@ -# Created by .ignore support plugin (hsz.mobi) -### Go template -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof - -.idea diff --git a/vendor/github.com/alvaroloes/enumer/LICENSE b/vendor/github.com/alvaroloes/enumer/LICENSE deleted file mode 100644 index 96e32476cd..0000000000 --- a/vendor/github.com/alvaroloes/enumer/LICENSE +++ /dev/null @@ -1,26 +0,0 @@ -Copyright (c) 2018, Álvaro López Espinosa -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. \ No newline at end of file diff --git a/vendor/github.com/alvaroloes/enumer/README.md b/vendor/github.com/alvaroloes/enumer/README.md deleted file mode 100644 index 5fb0434ca3..0000000000 --- a/vendor/github.com/alvaroloes/enumer/README.md +++ /dev/null @@ -1,147 +0,0 @@ -# Enumer [![GoDoc](https://godoc.org/github.com/alvaroloes/enumer?status.svg)](https://godoc.org/github.com/alvaroloes/enumer) [![Go Report Card](https://goreportcard.com/badge/github.com/alvaroloes/enumer)](https://goreportcard.com/report/github.com/alvaroloes/enumer) [![cover.run go](https://cover.run/go/github.com/alvaroloes/enumer.svg?tag=golang-1.10)](https://cover.run/go/github.com/alvaroloes/enumer?tag=golang-1.10) -Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). -It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer). - -# Install - -``` -go get github.com/alvaroloes/enumer -``` - -## Generated functions and methods -When Enumer is applied to a type, it will generate: - -* The following basic methods/functions: - - * Method `String()`: returns the string representation of the enum value. This makes the enum conform -the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number. - * Function `String(s string)`: returns the enum value from its string representation. This is useful -when you need to read enum values from command line arguments, from a configuration file, or -from a REST API request... In short, from those places where using the real enum value (an integer) would -be almost meaningless or hard to trace or use by a human. - * Function `Values()`: returns a slice with all the values of the enum - * Method `IsA()`: returns true only if the current value is among the values of the enum. Useful for validations. -* When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make -the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. -* When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()`. These make -the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces. -**Note:** If you use your enum values as keys in a map and you encode the map as _JSON_, you need this flag set to true to properly -convert the map keys to json (strings). If not, the numeric values will be used instead -* When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()`. These make -the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces. -* When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated. -Useful when storing the enum in a database. - -For example, if we have an enum type called `Pill`, -```go -type Pill int - -const ( - Placebo Pill = iota - Aspirin - Ibuprofen - Paracetamol - Acetaminophen = Paracetamol -) -``` -executing `enumer -type=Pill -json` will generate a new file with four basic methods and two extra for JSON: -```go -func (i Pill) String() string { - //... -} - -func PillString(s string) (Pill, error) { - //... -} - -func PillValues() []Pill { - //... -} - -func (i Pill) IsAPill() bool { - //... -} - -func (i Pill) MarshalJSON() ([]byte, error) { - //... -} - -func (i *Pill) UnmarshalJSON(data []byte) error { - //... -} -``` -From now on, we can: -```go -// Convert any Pill value to string -var aspirinString string = Aspirin.String() -// (or use it in any place where a Stringer is accepted) -fmt.Println("I need ", Paracetamol) // Will print "I need Paracetamol" - -// Convert a string with the enum name to the corresponding enum value -pill, err := PillString("Ibuprofen") -if err != nil { - fmt.Println("Unrecognized pill: ", err) - return -} -// Now pill == Ibuprofen - -// Get all the values of the string -allPills := PillValues() -fmt.Println(allPills) // Will print [Placebo Aspirin Ibuprofen Paracetamol] - -// Check if a value belongs to the Pill enum values -var notAPill Pill = 42 -if (notAPill.IsAPill()) { - fmt.Println(notAPill, "is not a value of the Pill enum") -} - -// Marshal/unmarshal to/from json strings, either directly or automatically when -// the enum is a field of a struct -pillJSON := Aspirin.MarshalJSON() -// Now pillJSON == `"Aspirin"` -``` - -The generated code is exactly the same as the Stringer tool plus the mentioned additions, so you can use -**Enumer** where you are already using **Stringer** without any code change. - -## Transforming the string representation of the enum value - -By default, Enumer uses the same name of the enum value for generating the string representation (usually CamelCase in Go). - -```go -type MyType int - - ... - -name := MyTypeValue.String() // name => "MyTypeValue" -``` - -Sometimes you need to use some other string representation format than CamelCase (i.e. in JSON). - -To transform it from CamelCase to snake_case or kebab-case, you can use the `transform` flag. - -For example, the command `enumer -type=MyType -json -transform=snake` would generate the following string representation: - -```go -name := MyTypeValue.String() // name => "my_type_value" -``` -**Note**: The transformation only works form CamelCase to snake_case or kebab-case, not the other way around. - -## How to use -The usage of Enumer is the same as Stringer, so you can refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer) -for more information. - -There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any combination of them (i.e. `enumer -type=Pill -json -text`), - - -For enum string representation transformation the `transform` and `trimprefix` flags -were added (i.e. `enumer -type=MyType -json -transform=snake`). -Possible transform values are `snake` and `kebab` for transformation to snake_case and kebab-case accordingly. -The default value for `transform` flag is `noop` which means no transformation will be performed. - -If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before -it is transformed). If a name doesn't have the prefix it will be passed unchanged. - -## Inspiring projects -* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) -* [jsonenums](https://github.com/campoy/jsonenums) diff --git a/vendor/github.com/alvaroloes/enumer/enumer.go b/vendor/github.com/alvaroloes/enumer/enumer.go deleted file mode 100644 index 6c3d6fdfd1..0000000000 --- a/vendor/github.com/alvaroloes/enumer/enumer.go +++ /dev/null @@ -1,157 +0,0 @@ -package main - -import "fmt" - -// Arguments to format are: -// [1]: type name -const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name. -// Throws an error if the param is not part of the enum. -func %[1]sString(s string) (%[1]s, error) { - if val, ok := _%[1]sNameToValueMap[s]; ok { - return val, nil - } - return 0, fmt.Errorf("%%s does not belong to %[1]s values", s) -} -` - -// Arguments to format are: -// [1]: type name -const stringValuesMethod = `// %[1]sValues returns all values of the enum -func %[1]sValues() []%[1]s { - return _%[1]sValues -} -` - -// Arguments to format are: -// [1]: type name -const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise -func (i %[1]s) IsA%[1]s() bool { - for _, v := range _%[1]sValues { - if i == v { - return true - } - } - return false -} -` - -// Arguments to format are: -// [1]: type name -const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise -func (i %[1]s) IsA%[1]s() bool { - _, ok := _%[1]sMap[i] - return ok -} -` - -func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) { - // At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called - - // Print the slice of values - g.Printf("\nvar _%sValues = []%s{", typeName, typeName) - for _, values := range runs { - for _, value := range values { - g.Printf("\t%s, ", value.str) - } - } - g.Printf("}\n\n") - - // Print the map between name and value - g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName) - thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold - var n int - var runID string - for i, values := range runs { - if thereAreRuns { - runID = "_" + fmt.Sprintf("%d", i) - n = 0 - } else { - runID = "" - } - - for _, value := range values { - g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value) - n += len(value.name) - } - } - g.Printf("}\n\n") - - // Print the basic extra methods - g.Printf(stringNameToValueMethod, typeName) - g.Printf(stringValuesMethod, typeName) - if len(runs) < runsThreshold { - g.Printf(stringBelongsMethodLoop, typeName) - } else { // There is a map of values, the code is simpler then - g.Printf(stringBelongsMethodSet, typeName) - } -} - -// Arguments to format are: -// [1]: type name -const jsonMethods = ` -// MarshalJSON implements the json.Marshaler interface for %[1]s -func (i %[1]s) MarshalJSON() ([]byte, error) { - return json.Marshal(i.String()) -} - -// UnmarshalJSON implements the json.Unmarshaler interface for %[1]s -func (i *%[1]s) UnmarshalJSON(data []byte) error { - var s string - if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("%[1]s should be a string, got %%s", data) - } - - var err error - *i, err = %[1]sString(s) - return err -} -` - -func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThreshold int) { - g.Printf(jsonMethods, typeName) -} - -// Arguments to format are: -// [1]: type name -const textMethods = ` -// MarshalText implements the encoding.TextMarshaler interface for %[1]s -func (i %[1]s) MarshalText() ([]byte, error) { - return []byte(i.String()), nil -} - -// UnmarshalText implements the encoding.TextUnmarshaler interface for %[1]s -func (i *%[1]s) UnmarshalText(text []byte) error { - var err error - *i, err = %[1]sString(string(text)) - return err -} -` - -func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThreshold int) { - g.Printf(textMethods, typeName) -} - -// Arguments to format are: -// [1]: type name -const yamlMethods = ` -// MarshalYAML implements a YAML Marshaler for %[1]s -func (i %[1]s) MarshalYAML() (interface{}, error) { - return i.String(), nil -} - -// UnmarshalYAML implements a YAML Unmarshaler for %[1]s -func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error { - var s string - if err := unmarshal(&s); err != nil { - return err - } - - var err error - *i, err = %[1]sString(s) - return err -} -` - -func (g *Generator) buildYAMLMethods(runs [][]Value, typeName string, runsThreshold int) { - g.Printf(yamlMethods, typeName) -} diff --git a/vendor/github.com/alvaroloes/enumer/sql.go b/vendor/github.com/alvaroloes/enumer/sql.go deleted file mode 100644 index 67d5354eb9..0000000000 --- a/vendor/github.com/alvaroloes/enumer/sql.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -// Arguments to format are: -// [1]: type name -const valueMethod = `func (i %[1]s) Value() (driver.Value, error) { - return i.String(), nil -} -` - -const scanMethod = `func (i *%[1]s) Scan(value interface{}) error { - if value == nil { - return nil - } - - str, ok := value.(string) - if !ok { - bytes, ok := value.([]byte) - if !ok { - return fmt.Errorf("value is not a byte slice") - } - - str = string(bytes[:]) - } - - val, err := %[1]sString(str) - if err != nil { - return err - } - - *i = val - return nil -} -` - -func (g *Generator) addValueAndScanMethod(typeName string) { - g.Printf("\n") - g.Printf(valueMethod, typeName) - g.Printf("\n\n") - g.Printf(scanMethod, typeName) -} diff --git a/vendor/github.com/alvaroloes/enumer/stringer.go b/vendor/github.com/alvaroloes/enumer/stringer.go deleted file mode 100644 index 26ff247d52..0000000000 --- a/vendor/github.com/alvaroloes/enumer/stringer.go +++ /dev/null @@ -1,738 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build go1.5 - -//Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). -//It started as a fork of Rob Pike’s Stringer tool -// -//Please visit http://github.com/alvaroloes/enumer for a comprehensive documentation -package main - -import ( - "bytes" - "flag" - "fmt" - "go/ast" - exact "go/constant" - "go/format" - "go/importer" - "go/token" - "go/types" - "golang.org/x/tools/go/packages" - "io/ioutil" - "log" - "os" - "path/filepath" - "sort" - "strings" - - "github.com/pascaldekloe/name" -) - -type arrayFlags []string - -func (af arrayFlags) String() string { - return strings.Join(af, "") -} - -func (af *arrayFlags) Set(value string) error { - *af = append(*af, value) - return nil -} - -var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") - json = flag.Bool("json", false, "if true, json marshaling methods will be generated. Default: false") - yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false") - text = flag.Bool("text", false, "if true, text marshaling methods will be generated. Default: false") - output = flag.String("output", "", "output file name; default srcdir/_string.go") - transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop") - trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"") - lineComment = flag.Bool("linecomment", false, "use line comment text as printed text when present") -) - -var comments arrayFlags - -func init() { - flag.Var(&comments, "comment", "comments to include in generated code, can repeat. Default: \"\"") -} - -// Usage is a replacement usage function for the flags package. -func Usage() { - fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - fmt.Fprintf(os.Stderr, "\tenumer [flags] -type T [directory]\n") - fmt.Fprintf(os.Stderr, "\tenumer [flags] -type T files... # Must be a single package\n") - fmt.Fprintf(os.Stderr, "For more information, see:\n") - fmt.Fprintf(os.Stderr, "\thttps://github.com/alvaroloes/enumer\n") - fmt.Fprintf(os.Stderr, "Flags:\n") - flag.PrintDefaults() -} - -func main() { - log.SetFlags(0) - log.SetPrefix("enumer: ") - flag.Usage = Usage - flag.Parse() - if len(*typeNames) == 0 { - flag.Usage() - os.Exit(2) - } - types := strings.Split(*typeNames, ",") - - // We accept either one directory or a list of files. Which do we have? - args := flag.Args() - if len(args) == 0 { - // Default: process whole package in current directory. - args = []string{"."} - } - - // Parse the package once. - var ( - dir string - g Generator - ) - - if len(args) == 1 && isDirectory(args[0]) { - dir = args[0] - } else { - dir = filepath.Dir(args[0]) - } - - g.parsePackage(args) - - // Print the header and package clause. - g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) - g.Printf("\n") - g.Printf("// %s\n", comments.String()) - g.Printf("package %s", g.pkg.name) - g.Printf("\n") - g.Printf("import (\n") - g.Printf("\t\"fmt\"\n") - if *sql { - g.Printf("\t\"database/sql/driver\"\n") - } - if *json { - g.Printf("\t\"encoding/json\"\n") - } - g.Printf(")\n") - - // Run generate for each type. - for _, typeName := range types { - g.generate(typeName, *json, *yaml, *sql, *text, *transformMethod, *trimPrefix, *lineComment) - } - - // Format the output. - src := g.format() - - // Figure out filename to write to - outputName := *output - if outputName == "" { - baseName := fmt.Sprintf("%s_enumer.go", types[0]) - outputName = filepath.Join(dir, strings.ToLower(baseName)) - } - - // Write to tmpfile first - tmpName := fmt.Sprintf("%s_enumer_", filepath.Base(types[0])) - tmpFile, err := ioutil.TempFile(filepath.Dir(types[0]), tmpName) - if err != nil { - log.Fatalf("creating temporary file for output: %s", err) - } - _, err = tmpFile.Write(src) - if err != nil { - tmpFile.Close() - os.Remove(tmpFile.Name()) - log.Fatalf("writing output: %s", err) - } - tmpFile.Close() - - // Rename tmpfile to output file - err = os.Rename(tmpFile.Name(), outputName) - if err != nil { - log.Fatalf("moving tempfile to output file: %s", err) - } -} - -// isDirectory reports whether the named file is a directory. -func isDirectory(name string) bool { - info, err := os.Stat(name) - if err != nil { - log.Fatal(err) - } - return info.IsDir() -} - -// Generator holds the state of the analysis. Primarily used to buffer -// the output for format.Source. -type Generator struct { - buf bytes.Buffer // Accumulated output. - pkg *Package // Package we are scanning. -} - -// Printf prints the string to the output -func (g *Generator) Printf(format string, args ...interface{}) { - fmt.Fprintf(&g.buf, format, args...) -} - -// File holds a single parsed file and associated data. -type File struct { - pkg *Package // Package to which this file belongs. - file *ast.File // Parsed AST. - // These fields are reset for each type being generated. - typeName string // Name of the constant type. - values []Value // Accumulator for constant values of that type. -} - -// Package holds information about a Go package -type Package struct { - dir string - name string - defs map[*ast.Ident]types.Object - files []*File - typesPkg *types.Package -} - -//// parsePackageDir parses the package residing in the directory. -//func (g *Generator) parsePackageDir(directory string) { -// pkg, err := build.Default.ImportDir(directory, 0) -// if err != nil { -// log.Fatalf("cannot process directory %s: %s", directory, err) -// } -// var names []string -// names = append(names, pkg.GoFiles...) -// names = append(names, pkg.CgoFiles...) -// // TODO: Need to think about constants in test files. Maybe write type_string_test.go -// // in a separate pass? For later. -// // names = append(names, pkg.TestGoFiles...) // These are also in the "foo" package. -// names = append(names, pkg.SFiles...) -// names = prefixDirectory(directory, names) -// g.parsePackage(directory, names, nil) -//} -// -//// parsePackageFiles parses the package occupying the named files. -//func (g *Generator) parsePackageFiles(names []string) { -// g.parsePackage(".", names, nil) -//} -// -//// prefixDirectory places the directory name on the beginning of each name in the list. -//func prefixDirectory(directory string, names []string) []string { -// if directory == "." { -// return names -// } -// ret := make([]string, len(names)) -// for i, name := range names { -// ret[i] = filepath.Join(directory, name) -// } -// return ret -//} - -//// parsePackage analyzes the single package constructed from the named files. -//// If text is non-nil, it is a string to be used instead of the content of the file, -//// to be used for testing. parsePackage exits if there is an error. -//func (g *Generator) parsePackage(directory string, names []string, text interface{}) { -// var files []*File -// var astFiles []*ast.File -// g.pkg = new(Package) -// fs := token.NewFileSet() -// for _, name := range names { -// if !strings.HasSuffix(name, ".go") { -// continue -// } -// parsedFile, err := parser.ParseFile(fs, name, text, 0) -// if err != nil { -// log.Fatalf("parsing package: %s: %s", name, err) -// } -// astFiles = append(astFiles, parsedFile) -// files = append(files, &File{ -// file: parsedFile, -// pkg: g.pkg, -// }) -// } -// if len(astFiles) == 0 { -// log.Fatalf("%s: no buildable Go files", directory) -// } -// g.pkg.name = astFiles[0].Name.Name -// g.pkg.files = files -// g.pkg.dir = directory -// // Type check the package. -// g.pkg.check(fs, astFiles) -//} - -// parsePackage analyzes the single package constructed from the patterns and tags. -// parsePackage exits if there is an error. -func (g *Generator) parsePackage(patterns []string) { - cfg := &packages.Config{ - Mode: packages.LoadSyntax, - // TODO: Need to think about constants in test files. Maybe write type_string_test.go - // in a separate pass? For later. - Tests: false, - } - pkgs, err := packages.Load(cfg, patterns...) - if err != nil { - log.Fatal(err) - } - if len(pkgs) != 1 { - log.Fatalf("error: %d packages found", len(pkgs)) - } - g.addPackage(pkgs[0]) -} - -// addPackage adds a type checked Package and its syntax files to the generator. -func (g *Generator) addPackage(pkg *packages.Package) { - g.pkg = &Package{ - name: pkg.Name, - defs: pkg.TypesInfo.Defs, - files: make([]*File, len(pkg.Syntax)), - } - - for i, file := range pkg.Syntax { - g.pkg.files[i] = &File{ - file: file, - pkg: g.pkg, - } - } -} - -// check type-checks the package. The package must be OK to proceed. -func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { - pkg.defs = make(map[*ast.Ident]types.Object) - config := types.Config{Importer: importer.Default(), FakeImportC: true} - info := &types.Info{ - Defs: pkg.defs, - } - typesPkg, err := config.Check(pkg.dir, fs, astFiles, info) - if err != nil { - log.Fatalf("checking package: %s", err) - } - pkg.typesPkg = typesPkg -} - -func (g *Generator) transformValueNames(values []Value, transformMethod string) { - var sep rune - switch transformMethod { - case "snake": - sep = '_' - case "kebab": - sep = '-' - default: - return - } - - for i := range values { - values[i].name = strings.ToLower(name.Delimit(values[i].name, sep)) - } -} - -// trimValueNames removes a prefix from each name -func (g *Generator) trimValueNames(values []Value, prefix string) { - for i := range values { - values[i].name = strings.TrimPrefix(values[i].name, prefix) - } -} - -func (g *Generator) replaceValuesWithLineComment(values []Value) { - for i, val := range values { - if val.comment != "" { - values[i].name = val.comment - } - } -} - -// generate produces the String method for the named type. -func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText bool, transformMethod string, trimPrefix string, lineComment bool) { - values := make([]Value, 0, 100) - for _, file := range g.pkg.files { - // Set the state for this run of the walker. - file.typeName = typeName - file.values = nil - if file.file != nil { - ast.Inspect(file.file, file.genDecl) - values = append(values, file.values...) - } - } - - if len(values) == 0 { - log.Fatalf("no values defined for type %s", typeName) - } - - g.trimValueNames(values, trimPrefix) - - g.transformValueNames(values, transformMethod) - - if lineComment { - g.replaceValuesWithLineComment(values) - } - - runs := splitIntoRuns(values) - // The decision of which pattern to use depends on the number of - // runs in the numbers. If there's only one, it's easy. For more than - // one, there's a tradeoff between complexity and size of the data - // and code vs. the simplicity of a map. A map takes more space, - // but so does the code. The decision here (crossover at 10) is - // arbitrary, but considers that for large numbers of runs the cost - // of the linear scan in the switch might become important, and - // rather than use yet another algorithm such as binary search, - // we punt and use a map. In any case, the likelihood of a map - // being necessary for any realistic example other than bitmasks - // is very low. And bitmasks probably deserve their own analysis, - // to be done some other day. - const runsThreshold = 10 - switch { - case len(runs) == 1: - g.buildOneRun(runs, typeName) - case len(runs) <= runsThreshold: - g.buildMultipleRuns(runs, typeName) - default: - g.buildMap(runs, typeName) - } - - g.buildBasicExtras(runs, typeName, runsThreshold) - if includeJSON { - g.buildJSONMethods(runs, typeName, runsThreshold) - } - if includeText { - g.buildTextMethods(runs, typeName, runsThreshold) - } - if includeYAML { - g.buildYAMLMethods(runs, typeName, runsThreshold) - } - if includeSQL { - g.addValueAndScanMethod(typeName) - } -} - -// splitIntoRuns breaks the values into runs of contiguous sequences. -// For example, given 1,2,3,5,6,7 it returns {1,2,3},{5,6,7}. -// The input slice is known to be non-empty. -func splitIntoRuns(values []Value) [][]Value { - // We use stable sort so the lexically first name is chosen for equal elements. - sort.Stable(byValue(values)) - // Remove duplicates. Stable sort has put the one we want to print first, - // so use that one. The String method won't care about which named constant - // was the argument, so the first name for the given value is the only one to keep. - // We need to do this because identical values would cause the switch or map - // to fail to compile. - j := 1 - for i := 1; i < len(values); i++ { - if values[i].value != values[i-1].value { - values[j] = values[i] - j++ - } - } - values = values[:j] - runs := make([][]Value, 0, 10) - for len(values) > 0 { - // One contiguous sequence per outer loop. - i := 1 - for i < len(values) && values[i].value == values[i-1].value+1 { - i++ - } - runs = append(runs, values[:i]) - values = values[i:] - } - return runs -} - -// format returns the gofmt-ed contents of the Generator's buffer. -func (g *Generator) format() []byte { - src, err := format.Source(g.buf.Bytes()) - if err != nil { - // Should never happen, but can arise when developing this code. - // The user can compile the output to see the error. - log.Printf("warning: internal error: invalid Go generated: %s", err) - log.Printf("warning: compile the package to analyze the error") - return g.buf.Bytes() - } - return src -} - -// Value represents a declared constant. -type Value struct { - name string // The name of the constant after transformation (i.e. camel case => snake case) - // The value is stored as a bit pattern alone. The boolean tells us - // whether to interpret it as an int64 or a uint64; the only place - // this matters is when sorting. - // Much of the time the str field is all we need; it is printed - // by Value.String. - value uint64 // Will be converted to int64 when needed. - signed bool // Whether the constant is a signed type. - str string // The string representation given by the "go/exact" package. - comment string // The comment on the right of the constant -} - -func (v *Value) String() string { - return v.str -} - -// byValue lets us sort the constants into increasing order. -// We take care in the Less method to sort in signed or unsigned order, -// as appropriate. -type byValue []Value - -func (b byValue) Len() int { return len(b) } -func (b byValue) Swap(i, j int) { b[i], b[j] = b[j], b[i] } -func (b byValue) Less(i, j int) bool { - if b[i].signed { - return int64(b[i].value) < int64(b[j].value) - } - return b[i].value < b[j].value -} - -// genDecl processes one declaration clause. -func (f *File) genDecl(node ast.Node) bool { - decl, ok := node.(*ast.GenDecl) - if !ok || decl.Tok != token.CONST { - // We only care about const declarations. - return true - } - // The name of the type of the constants we are declaring. - // Can change if this is a multi-element declaration. - typ := "" - // Loop over the elements of the declaration. Each element is a ValueSpec: - // a list of names possibly followed by a type, possibly followed by values. - // If the type and value are both missing, we carry down the type (and value, - // but the "go/types" package takes care of that). - for _, spec := range decl.Specs { - vspec := spec.(*ast.ValueSpec) // Guaranteed to succeed as this is CONST. - if vspec.Type == nil && len(vspec.Values) > 0 { - // "X = 1". With no type but a value, the constant is untyped. - // Skip this vspec and reset the remembered type. - typ = "" - continue - } - if vspec.Type != nil { - // "X T". We have a type. Remember it. - ident, ok := vspec.Type.(*ast.Ident) - if !ok { - continue - } - typ = ident.Name - } - if typ != f.typeName { - // This is not the type we're looking for. - continue - } - // We now have a list of names (from one line of source code) all being - // declared with the desired type. - // Grab their names and actual values and store them in f.values. - for _, name := range vspec.Names { - if name.Name == "_" { - continue - } - // This dance lets the type checker find the values for us. It's a - // bit tricky: look up the object declared by the name, find its - // types.Const, and extract its value. - obj, ok := f.pkg.defs[name] - if !ok { - log.Fatalf("no value for constant %s", name) - } - info := obj.Type().Underlying().(*types.Basic).Info() - if info&types.IsInteger == 0 { - log.Fatalf("can't handle non-integer constant type %s", typ) - } - value := obj.(*types.Const).Val() // Guaranteed to succeed as this is CONST. - if value.Kind() != exact.Int { - log.Fatalf("can't happen: constant is not an integer %s", name) - } - i64, isInt := exact.Int64Val(value) - u64, isUint := exact.Uint64Val(value) - if !isInt && !isUint { - log.Fatalf("internal error: value of %s is not an integer: %s", name, value.String()) - } - if !isInt { - u64 = uint64(i64) - } - comment := "" - if c := vspec.Comment; c != nil && len(c.List) == 1 { - comment = strings.TrimSpace(c.Text()) - } - - v := Value{ - name: name.Name, - value: u64, - signed: info&types.IsUnsigned == 0, - str: value.String(), - comment: comment, - } - f.values = append(f.values, v) - } - } - return false -} - -// Helpers - -// usize returns the number of bits of the smallest unsigned integer -// type that will hold n. Used to create the smallest possible slice of -// integers to use as indexes into the concatenated strings. -func usize(n int) int { - switch { - case n < 1<<8: - return 8 - case n < 1<<16: - return 16 - default: - // 2^32 is enough constants for anyone. - return 32 - } -} - -// declareIndexAndNameVars declares the index slices and concatenated names -// strings representing the runs of values. -func (g *Generator) declareIndexAndNameVars(runs [][]Value, typeName string) { - var indexes, names []string - for i, run := range runs { - index, name := g.createIndexAndNameDecl(run, typeName, fmt.Sprintf("_%d", i)) - indexes = append(indexes, index) - names = append(names, name) - } - g.Printf("const (\n") - for _, name := range names { - g.Printf("\t%s\n", name) - } - g.Printf(")\n\n") - g.Printf("var (") - for _, index := range indexes { - g.Printf("\t%s\n", index) - } - g.Printf(")\n\n") -} - -// declareIndexAndNameVar is the single-run version of declareIndexAndNameVars -func (g *Generator) declareIndexAndNameVar(run []Value, typeName string) { - index, name := g.createIndexAndNameDecl(run, typeName, "") - g.Printf("const %s\n", name) - g.Printf("var %s\n", index) -} - -// createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var". -func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string) { - b := new(bytes.Buffer) - indexes := make([]int, len(run)) - for i := range run { - b.WriteString(run[i].name) - indexes[i] = b.Len() - } - nameConst := fmt.Sprintf("_%sName%s = %q", typeName, suffix, b.String()) - nameLen := b.Len() - b.Reset() - fmt.Fprintf(b, "_%sIndex%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen)) - for i, v := range indexes { - if i > 0 { - fmt.Fprintf(b, ", ") - } - fmt.Fprintf(b, "%d", v) - } - fmt.Fprintf(b, "}") - return b.String(), nameConst -} - -// declareNameVars declares the concatenated names string representing all the values in the runs. -func (g *Generator) declareNameVars(runs [][]Value, typeName string, suffix string) { - g.Printf("const _%sName%s = \"", typeName, suffix) - for _, run := range runs { - for i := range run { - g.Printf("%s", run[i].name) - } - } - g.Printf("\"\n") -} - -// buildOneRun generates the variables and String method for a single run of contiguous values. -func (g *Generator) buildOneRun(runs [][]Value, typeName string) { - values := runs[0] - g.Printf("\n") - g.declareIndexAndNameVar(values, typeName) - // The generated code is simple enough to write as a Printf format. - lessThanZero := "" - if values[0].signed { - lessThanZero = "i < 0 || " - } - if values[0].value == 0 { // Signed or unsigned, 0 is still 0. - g.Printf(stringOneRun, typeName, usize(len(values)), lessThanZero) - } else { - g.Printf(stringOneRunWithOffset, typeName, values[0].String(), usize(len(values)), lessThanZero) - } -} - -// Arguments to format are: -// [1]: type name -// [2]: size of index element (8 for uint8 etc.) -// [3]: less than zero check (for signed types) -const stringOneRun = `func (i %[1]s) String() string { - if %[3]si >= %[1]s(len(_%[1]sIndex)-1) { - return fmt.Sprintf("%[1]s(%%d)", i) - } - return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]] -} -` - -// Arguments to format are: -// [1]: type name -// [2]: lowest defined value for type, as a string -// [3]: size of index element (8 for uint8 etc.) -// [4]: less than zero check (for signed types) -/* - */ -const stringOneRunWithOffset = `func (i %[1]s) String() string { - i -= %[2]s - if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { - return fmt.Sprintf("%[1]s(%%d)", i + %[2]s) - } - return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]] -} -` - -// buildMultipleRuns generates the variables and String method for multiple runs of contiguous values. -// For this pattern, a single Printf format won't do. -func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { - g.Printf("\n") - g.declareIndexAndNameVars(runs, typeName) - g.Printf("func (i %s) String() string {\n", typeName) - g.Printf("\tswitch {\n") - for i, values := range runs { - if len(values) == 1 { - g.Printf("\tcase i == %s:\n", &values[0]) - g.Printf("\t\treturn _%sName_%d\n", typeName, i) - continue - } - g.Printf("\tcase %s <= i && i <= %s:\n", &values[0], &values[len(values)-1]) - if values[0].value != 0 { - g.Printf("\t\ti -= %s\n", &values[0]) - } - g.Printf("\t\treturn _%sName_%d[_%sIndex_%d[i]:_%sIndex_%d[i+1]]\n", - typeName, i, typeName, i, typeName, i) - } - g.Printf("\tdefault:\n") - g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName) - g.Printf("\t}\n") - g.Printf("}\n") -} - -// buildMap handles the case where the space is so sparse a map is a reasonable fallback. -// It's a rare situation but has simple code. -func (g *Generator) buildMap(runs [][]Value, typeName string) { - g.Printf("\n") - g.declareNameVars(runs, typeName, "") - g.Printf("\nvar _%sMap = map[%s]string{\n", typeName, typeName) - n := 0 - for _, values := range runs { - for _, value := range values { - g.Printf("\t%s: _%sName[%d:%d],\n", &value, typeName, n, n+len(value.name)) - n += len(value.name) - } - } - g.Printf("}\n\n") - g.Printf(stringMap, typeName) -} - -// Argument to format is the type name. -const stringMap = `func (i %[1]s) String() string { - if str, ok := _%[1]sMap[i]; ok { - return str - } - return fmt.Sprintf("%[1]s(%%d)", i) -} -` diff --git a/vendor/github.com/nginx/agent/sdk/v2/client/client.go b/vendor/github.com/nginx/agent/sdk/v2/client/client.go index 6c79e13098..a0d133ea1f 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/client/client.go +++ b/vendor/github.com/nginx/agent/sdk/v2/client/client.go @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -//go:generate enumer -type=MsgClassification -text -yaml -json -transform=snake -trimprefix=MsgClassification - package client import ( diff --git a/vendor/github.com/pascaldekloe/name/.travis.yml b/vendor/github.com/pascaldekloe/name/.travis.yml deleted file mode 100644 index 4f2ee4d973..0000000000 --- a/vendor/github.com/pascaldekloe/name/.travis.yml +++ /dev/null @@ -1 +0,0 @@ -language: go diff --git a/vendor/github.com/pascaldekloe/name/LICENSE b/vendor/github.com/pascaldekloe/name/LICENSE deleted file mode 100644 index 88188f77aa..0000000000 --- a/vendor/github.com/pascaldekloe/name/LICENSE +++ /dev/null @@ -1,5 +0,0 @@ -To the extent possible under law, Pascal S. de Kloe has waived all -copyright and related or neighboring rights to name. This work is -published from The Netherlands. - -https://creativecommons.org/publicdomain/zero/1.0/legalcode diff --git a/vendor/github.com/pascaldekloe/name/README.md b/vendor/github.com/pascaldekloe/name/README.md deleted file mode 100644 index 091a43c3c3..0000000000 --- a/vendor/github.com/pascaldekloe/name/README.md +++ /dev/null @@ -1,51 +0,0 @@ -[![API Documentation](https://godoc.org/github.com/pascaldekloe/name?status.svg)](https://godoc.org/github.com/pascaldekloe/name) -[![Build Status](https://travis-ci.org/pascaldekloe/name.svg?branch=master)](https://travis-ci.org/pascaldekloe/name) - -## About - -… a naming-convention library for the Go programming language. -The two categories are delimiter-separated and letter case-separated words. -Each of the formatting functions support both techniques for input, without -any context. - -This is free and unencumbered software released into the -[public domain](http://creativecommons.org/publicdomain/zero/1.0). - - -### Inspiration - -* `name.CamelCase("pascal case", true)` returns “PascalCase” -* `name.CamelCase("snake_to_camel AND CamelToCamel?", false)` returns “snakeToCamelANDCamelToCamel” -* `name.Delimit("* All Hype is aGoodThing (TM)", '-')` returns “all-hype-is-a-good-thing-TM” -* `name.DotSeparated("WebCrawler#socketTimeout")` returns “web.crawler.socket.timeout” - - -### Performance - -The following results were measured with Go 1.15 on an Intel i5-7500. - -``` -name time/op -Cases/a2B/CamelCase-4 38.9ns ± 5% -Cases/a2B/snake_case-4 41.1ns ± 1% -Cases/foo-bar/CamelCase-4 58.0ns ± 6% -Cases/foo-bar/snake_case-4 67.0ns ± 1% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4 272ns ± 6% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4 324ns ± 1% - -name alloc/op -Cases/a2B/CamelCase-4 3.00B ± 0% -Cases/a2B/snake_case-4 4.00B ± 0% -Cases/foo-bar/CamelCase-4 8.00B ± 0% -Cases/foo-bar/snake_case-4 16.0B ± 0% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4 48.0B ± 0% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4 64.0B ± 0% - -name allocs/op -Cases/a2B/CamelCase-4 1.00 ± 0% -Cases/a2B/snake_case-4 1.00 ± 0% -Cases/foo-bar/CamelCase-4 1.00 ± 0% -Cases/foo-bar/snake_case-4 1.00 ± 0% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4 1.00 ± 0% -Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4 1.00 ± 0% -``` diff --git a/vendor/github.com/pascaldekloe/name/case.go b/vendor/github.com/pascaldekloe/name/case.go deleted file mode 100644 index a75b2aee3d..0000000000 --- a/vendor/github.com/pascaldekloe/name/case.go +++ /dev/null @@ -1,129 +0,0 @@ -// Package name implements various naming conventions. The two categories are -// delimiter-separated and letter case-separated words. Each of the formatting -// functions support both techniques for input, without any context. -package name - -import ( - "strings" - "unicode" -) - -// CamelCase returns the medial capitals form of the words in s. -// Words consist of Unicode letters and/or numbers in any order. -// Upper case sequences [abbreviations] are preserved. -// -// Argument upper forces the letter case for the first rune. -// Use true for UpperCamelCase, a.k.a. PascalCase. -// Use false for lowerCamelCase, a.k.a. dromedaryCase. -// -// BUG(pascaldekloe): Abbreviations at the beginning of a name -// may look odd in lowerCamelCase, i.e., "tCPConn". -// -// BUG(pascaldekloe): CamelCase concatenates abbreviations by -// design, i.e., "DB-API" becomes "DBAPI". -func CamelCase(s string, upper bool) string { - var b strings.Builder - b.Grow(len(s)) - - // The conversion keeps any camel-casing as is. - for _, r := range s { - switch { - case unicode.IsLetter(r): - if upper { - r = unicode.ToUpper(r) - } else if b.Len() == 0 { - // force only on beginning of name - r = unicode.ToLower(r) - } - - fallthrough - case unicode.IsNumber(r): - b.WriteRune(r) - upper = false // mark continuation - - default: - // delimiter found - upper = true // mark begin - } - } - - return b.String() -} - -// SnakeCase returns Delimit(s, '_'), a.k.a. the snake_case. -func SnakeCase(s string) string { - return Delimit(s, '_') -} - -// DotSeparated returns Delimit(s, '.'), a.k.a. the dot notation. -func DotSeparated(s string) string { - return Delimit(s, '.') -} - -// Delimit returns the words in s delimited with separator sep. -// Words consist of Unicode letters and/or numbers in any order. -// Upper case sequences [abbreviations] are preserved. -// Use strings.ToLower or ToUpper to enforce one letter case. -func Delimit(s string, sep rune) string { - var b strings.Builder - b.Grow(len(s) + (len(s)+1)/4) - - var last rune // previous rune is a pending write - var wordLen int // number of runes in word up until last - for _, r := range s { - switch { - case wordLen == 0: - if unicode.IsLetter(r) || unicode.IsNumber(r) { - if b.Len() == 0 { // special case - last = unicode.ToUpper(r) - } else { // delimit previous word - b.WriteRune(sep) - last = r - } - wordLen = 1 - } - - continue - - case unicode.IsUpper(r): - if !unicode.IsUpper(last) { - if b.Len() != 0 { - b.WriteRune(last) // end of word - last = sep // enqueue separator instead - wordLen = 0 // r is new begin - } - } - - case unicode.IsLetter(r): // lower-case - if unicode.IsUpper(last) { - if wordLen > 1 { - // delimit previous word - b.WriteRune(sep) - wordLen = 1 - } - last = unicode.ToLower(last) - } - - case !unicode.IsNumber(r): - // delimiter found - if wordLen != 0 { - // flush pending - b.WriteRune(last) - wordLen = 0 - } - - continue - } - - b.WriteRune(last) - last = r - wordLen++ - } - - if wordLen != 0 { - // flush pending - b.WriteRune(last) - } - - return b.String() -} diff --git a/vendor/modules.txt b/vendor/modules.txt index c440b71fe3..25c0a32fe1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -83,9 +83,6 @@ github.com/alexkohler/prealloc/pkg # github.com/alingse/asasalint v0.0.11 ## explicit; go 1.18 github.com/alingse/asasalint -# github.com/alvaroloes/enumer v1.1.2 -## explicit; go 1.12 -github.com/alvaroloes/enumer # github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d ## explicit; go 1.13 github.com/asaskevich/govalidator @@ -1134,9 +1131,6 @@ github.com/opencontainers/image-spec/specs-go/v1 # github.com/orcaman/concurrent-map v1.0.0 ## explicit github.com/orcaman/concurrent-map -# github.com/pascaldekloe/name v1.0.1 -## explicit; go 1.10 -github.com/pascaldekloe/name # github.com/pelletier/go-toml/v2 v2.0.6 ## explicit; go 1.16 github.com/pelletier/go-toml/v2 From ec224001081d28fb8c954e73aa29435c31359e76 Mon Sep 17 00:00:00 2001 From: Dean Coakley Date: Thu, 29 Jun 2023 11:13:09 +0100 Subject: [PATCH 4/4] Add timeout to integration tests setup (#360) --- test/integration/utils/test_container_utils.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/integration/utils/test_container_utils.go b/test/integration/utils/test_container_utils.go index 5d052c9512..14c288ae96 100644 --- a/test/integration/utils/test_container_utils.go +++ b/test/integration/utils/test_container_utils.go @@ -5,6 +5,7 @@ import ( "io" "os" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -13,6 +14,8 @@ import ( wait "github.com/testcontainers/testcontainers-go/wait" ) +const agentServiceTimeout = 20 * time.Second + // SetupTestContainerWithAgent sets up a container with nginx and nginx-agent installed func SetupTestContainerWithAgent(t *testing.T) *testcontainers.DockerContainer { comp, err := compose.NewDockerCompose(os.Getenv("DOCKER_COMPOSE_FILE")) @@ -27,7 +30,7 @@ func SetupTestContainerWithAgent(t *testing.T) *testcontainers.DockerContainer { t.Cleanup(cancel) require.NoError(t, - comp.WaitForService("agent", wait.ForLog("OneTimeRegistration completed")).WithEnv( + comp.WaitForService("agent", wait.ForLog("OneTimeRegistration completed").WithStartupTimeout(agentServiceTimeout)).WithEnv( map[string]string{ "PACKAGE_NAME": os.Getenv("PACKAGE_NAME"), "PACKAGES_REPO": os.Getenv("PACKAGES_REPO"), @@ -56,7 +59,7 @@ func SetupTestContainerWithoutAgent(t *testing.T) *testcontainers.DockerContaine ctxCancel, cancel := context.WithCancel(ctx) t.Cleanup(cancel) - require.NoError(t, comp.WaitForService("agent", wait.ForHTTP("/")).WithEnv( + require.NoError(t, comp.WaitForService("agent", wait.ForHTTP("/").WithStartupTimeout(agentServiceTimeout)).WithEnv( map[string]string{ "PACKAGE_NAME": os.Getenv("PACKAGE_NAME"), "PACKAGES_REPO": os.Getenv("PACKAGES_REPO"),