From 116f2063a55e1759ea29abd72b8a2aaca6362a06 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 4 Feb 2021 15:23:34 +0530 Subject: [PATCH 01/16] intergration test of infra-proxy-service Signed-off-by: Vivek Singh --- .studio/infra-proxy-service | 8 +++ .../integration_test/clients_test.go | 23 ++++++ .../integration_test/global_test.go | 70 +++++++++++++++++++ .../integration_test/suite_test.go | 33 +++++++++ 4 files changed, 134 insertions(+) create mode 100644 components/infra-proxy-service/integration_test/clients_test.go create mode 100644 components/infra-proxy-service/integration_test/global_test.go create mode 100644 components/infra-proxy-service/integration_test/suite_test.go diff --git a/.studio/infra-proxy-service b/.studio/infra-proxy-service index 27aab1ec650..7efab6a591b 100644 --- a/.studio/infra-proxy-service +++ b/.studio/infra-proxy-service @@ -11,6 +11,14 @@ function start_infra_proxy_service() { chef-automate dev deploy-some $HAB_ORIGIN/infra-proxy-service --with-deps } +document "infra_service_integration" < Date: Fri, 5 Feb 2021 18:13:40 +0530 Subject: [PATCH 02/16] more changes Signed-off-by: Vivek Singh --- .../cmd/infra-proxy-service/commands/serve.go | 16 +------ .../infra-proxy-service/config/service.go | 47 +++++++++++++++++++ .../integration_test/clients_test.go | 2 +- .../integration_test/global_test.go | 32 +++++-------- .../integration_test/suite_test.go | 4 +- 5 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 components/infra-proxy-service/config/service.go diff --git a/components/infra-proxy-service/cmd/infra-proxy-service/commands/serve.go b/components/infra-proxy-service/cmd/infra-proxy-service/commands/serve.go index 4172ea0d5c3..34137ea00cb 100644 --- a/components/infra-proxy-service/cmd/infra-proxy-service/commands/serve.go +++ b/components/infra-proxy-service/cmd/infra-proxy-service/commands/serve.go @@ -11,13 +11,13 @@ import ( secrets "github.com/chef/automate/api/external/secrets" "github.com/chef/automate/api/interservice/authz" + "github.com/chef/automate/components/infra-proxy-service/config" "github.com/chef/automate/components/infra-proxy-service/server" "github.com/chef/automate/components/infra-proxy-service/service" "github.com/chef/automate/components/infra-proxy-service/storage/postgres/migration" "github.com/chef/automate/lib/grpc/secureconn" "github.com/chef/automate/lib/logger" platform_config "github.com/chef/automate/lib/platform/config" - "github.com/chef/automate/lib/tls/certs" ) var serveCmd = &cobra.Command{ @@ -27,18 +27,6 @@ var serveCmd = &cobra.Command{ Args: cobra.ExactArgs(1), } -type config struct { - GRPC string `mapstructure:"grpc"` - LogFormat string `mapstructure:"log-format"` - LogLevel string `mapstructure:"log-level"` - certs.TLSConfig `mapstructure:"tls"` - PGURL string `mapstructure:"pg_url"` - Database string `mapstructure:"database"` - MigrationsPath string `mapstructure:"migrations-path"` - AuthzAddress string `mapstructure:"authz-address"` - SecretsAddress string `mapstructure:"secrets-address"` -} - func serve(cmd *cobra.Command, args []string) { cmd.PersistentFlags().StringP("log-level", "l", "info", "log level") cmd.PersistentFlags().StringP("log-format", "f", "text", "log format") @@ -53,7 +41,7 @@ func serve(cmd *cobra.Command, args []string) { fail(errors.Wrap(err, `Could not read config file. Please pass a config file as the only argument to this command.`)) } - cfg := config{} + cfg := config.Service{} if err := viper.Unmarshal(&cfg); err != nil { fail(errors.Wrap(err, "couldn't parse configuration file")) } diff --git a/components/infra-proxy-service/config/service.go b/components/infra-proxy-service/config/service.go new file mode 100644 index 00000000000..b450eed6d29 --- /dev/null +++ b/components/infra-proxy-service/config/service.go @@ -0,0 +1,47 @@ +package config + +import ( + "github.com/chef/automate/lib/tls/certs" + log "github.com/sirupsen/logrus" +) + +// Service config options +type Service struct { + GRPC string `mapstructure:"grpc"` + LogFormat string `mapstructure:"log-format"` + LogLevel string `mapstructure:"log-level"` + certs.TLSConfig `mapstructure:"tls"` + PGURL string `mapstructure:"pg_url"` + Database string `mapstructure:"database"` + MigrationsPath string `mapstructure:"migrations-path"` + AuthzAddress string `mapstructure:"authz-address"` + SecretsAddress string `mapstructure:"secrets-address"` +} + +// Default returns a blank Service instance with default parameters +func Default() *Service { + return &Service{ + GRPC: "127.0.0.1:10153", + LogLevel: "info", + MigrationsPath: "/src/components/infra-proxy-service/storage/postgres/schema/sql/", + } +} + +// SetLogLevel sets the log level for the service +func (s *Service) SetLogLevel() { + if s.LogLevel == "" { + return + } + + log.WithFields(log.Fields{ + "level": s.LogLevel, + }).Info("Setting log level") + + level, err := log.ParseLevel(s.LogLevel) + if err != nil { + log.WithField("level", s.LogLevel).WithError(err).Error("Using default level 'info'") + return + } + + log.SetLevel(level) +} diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go index 903a2369c34..301b54a37d1 100644 --- a/components/infra-proxy-service/integration_test/clients_test.go +++ b/components/infra-proxy-service/integration_test/clients_test.go @@ -16,7 +16,7 @@ func TestClientsReturnsEmptyList(t *testing.T) { req := request.Clients{} expected := new(response.Clients) - res, err := infraProxy.GetClients(ctx, *req) + res, err := infraProxy.GetClients(ctx, &req) fmt.Print(expected) fmt.Print(res) fmt.Print(err) diff --git a/components/infra-proxy-service/integration_test/global_test.go b/components/infra-proxy-service/integration_test/global_test.go index 356643e926d..22f0977ff22 100644 --- a/components/infra-proxy-service/integration_test/global_test.go +++ b/components/infra-proxy-service/integration_test/global_test.go @@ -1,14 +1,14 @@ package integration_test import ( - "fmt" "os" - "github.com/chef/automate/components/infra-proxy-service/config" - "github.com/chef/automate/components/infra-proxy-service/storage/postgres" - "github.com/pkg/errors" - "google.golang.org/grpc" + + "github.com/chef/automate/components/infra-proxy-service/config" + "github.com/chef/automate/components/infra-proxy-service/server" + "github.com/chef/automate/components/infra-proxy-service/service" + platform_config "github.com/chef/automate/lib/platform/config" ) // Global variables @@ -34,7 +34,7 @@ func (s *Suite) GlobalSetup() error { // newInfraProxyServer initializes a InfraProxyServer with the default config -func newInfraProxyServer() (*grpc.Server, error) { +func newInfraProxyServer() (*server.Server, error) { _, haveSvcName := os.LookupEnv(A2_SVC_NAME) _, haveSvcPath := os.LookupEnv(A2_SVC_PATH) if !(haveSvcName && haveSvcPath) { @@ -42,7 +42,6 @@ func newInfraProxyServer() (*grpc.Server, error) { _ = os.Setenv(A2_SVC_PATH, defaultA2ServicePath) } - fmt.Print("=================") uri, err := platform_config.PGURIFromEnvironment(pgDatabaseName) if err != nil { @@ -50,21 +49,16 @@ func newInfraProxyServer() (*grpc.Server, error) { } cfg := config.Default() - cfg.Postgres = config.Postgres{ - URI: uri, - Database: pgDatabaseName, - SchemaPath: "/src/components/infra-proxy-service/storage/postgres/schema/sql/", - } + cfg.PGURL = uri + cfg.Database = pgDatabaseName - err = postgres.DestructiveReMigrateForTest(&cfg.Postgres) + service, err := service.Start(cfg.LogLevel, migrationConfig, nil, nil, nil) if err != nil { - return nil, err + fail(errors.Wrap(err, "could not initialize storage")) } - srv := grpc.NewServer(cfg) - err = srv.ConnectPg() - if err != nil { - return nil, err - } + fail(server.GRPC(cfg.GRPC, service)) + + srv := server.NewServer(cfg) return srv, nil } \ No newline at end of file diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index f887c4c0d55..ec7ec1fb91f 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -1,7 +1,7 @@ package integration_test import ( - grpc "github.com/chef/automate/components/infra-proxy-service/server" + "github.com/chef/automate/components/infra-proxy-service/server" ) // Global variables @@ -13,7 +13,7 @@ var ( // ``` // res, err := infraProxy.GetOrgs(ctx, &req) // ``` - infraProxy *grpc.Server + infraProxy *server.Server ) const ( From 6b9ad42f82ac0d19bea5379962482d177716880e Mon Sep 17 00:00:00 2001 From: Kallol Roy Date: Sun, 7 Feb 2021 07:33:04 +0530 Subject: [PATCH 03/16] Sample integration test using http calls to api gateway Signed-off-by: Kallol Roy --- .../integration/infra_proxy_test.go | 79 +++++++++++++++++++ .../integration/suite_test.go | 18 +++-- 2 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 components/automate-gateway/integration/infra_proxy_test.go diff --git a/components/automate-gateway/integration/infra_proxy_test.go b/components/automate-gateway/integration/infra_proxy_test.go new file mode 100644 index 00000000000..fa1c1196a82 --- /dev/null +++ b/components/automate-gateway/integration/infra_proxy_test.go @@ -0,0 +1,79 @@ +package integration + +import ( + "bytes" + "crypto/tls" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "github.com/stretchr/testify/assert" +) + +type parsedResponse map[string]interface{} + +var client = NewClient() +var currenTimeStamp = time.Now().Second() + +func NewClient() *http.Client { + transport := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + return &http.Client{Transport: transport} +} + +func (suite *GatewayTestSuite) TestInfraProxyServersAPI() { + // add a mock server to Chef Infra + addServer(suite) + // check if the added server is available + getServers(suite) +} + +func addServer(suite *GatewayTestSuite) { + ipAddress := "128.170.178.154" + postBody, _ := json.Marshal(map[string]string{ + "id": fmt.Sprintf("chef-server-%d-id", currenTimeStamp), + "name": fmt.Sprintf("chef-server-%d", currenTimeStamp), + "fqdn": fmt.Sprintf("chef-server-%d.com", currenTimeStamp), + "ip_address": ipAddress, + }) + post, err := http.NewRequest("POST", "https://127.0.0.1/api/v0/infra/servers", bytes.NewBuffer(postBody)) + post.Header.Add("api-token", suite.automateAPIToken) + response, err := client.Do(post) + if assert.Nil(suite.T(), err, "Error sending request %v", err) { + assert.True(suite.T(), response.StatusCode == 200, "Expected Get Servers status code %d, got: %d", 200, response.StatusCode) + } +} + +func getServers(suite *GatewayTestSuite) { + get, err := http.NewRequest("GET", "https://127.0.0.1/api/v0/infra/servers", nil) + get.Header.Add("api-token", suite.automateAPIToken) + response, err := client.Do(get) + if assert.Nil(suite.T(), err, "Error sending request %v", err) { + assert.True(suite.T(), response.StatusCode == 200, "Expected Get Servers status code %d, got: %d", 200, response.StatusCode) + } + resp, err := parseResponseBody(response.Body) + servers := resp["servers"].([]interface{}) + found := false + for _, s := range servers { + m := s.(map[string]interface{}) + if m["fqdn"].(string) == fmt.Sprintf("chef-server-%d.com", currenTimeStamp) { + found = true + break + } + } + + assert.True(suite.T(), found, "Expected to get server %s but failed to receive the details", fmt.Sprintf("chef-server-%d.com", currenTimeStamp)) +} + +func parseResponseBody(body io.ReadCloser) (parsedResponse, error) { + m := make(map[string]interface{}) + err := json.NewDecoder(body).Decode(&m) + if err != nil { + return nil, err + } + + return m, err +} diff --git a/components/automate-gateway/integration/suite_test.go b/components/automate-gateway/integration/suite_test.go index 64d99738540..86a33cf9568 100644 --- a/components/automate-gateway/integration/suite_test.go +++ b/components/automate-gateway/integration/suite_test.go @@ -20,10 +20,11 @@ const ( ) type GatewayTestSuite struct { - ctx context.Context - target *acceptanceTarget - clients gateway.ClientsFactory - gwConn *grpc.ClientConn + ctx context.Context + target *acceptanceTarget + clients gateway.ClientsFactory + gwConn *grpc.ClientConn + automateAPIToken string suite.Suite } @@ -58,10 +59,11 @@ func NewGatewayTestSuite(ctx context.Context, t *testing.T, target *acceptanceTa } return &GatewayTestSuite{ - ctx: ctx, - target: target, - clients: clients, - gwConn: gwConn, + ctx: ctx, + target: target, + clients: clients, + gwConn: gwConn, + automateAPIToken: os.Getenv("AUTOMATE_API_TOKEN"), }, nil } From 8ed1e9471ba798b45ba99023f844bff75fab24c2 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 4 Feb 2021 15:23:34 +0530 Subject: [PATCH 04/16] intergration test of infra-proxy-service Signed-off-by: Vivek Singh --- .../infra-proxy-service/config/service.go | 120 +++++++++++++++--- components/infra-proxy-service/dev/config.yml | 2 +- .../integration_test/clients_test.go | 2 +- .../integration_test/global_test.go | 63 +++------ .../integration_test/suite_test.go | 47 +++++-- 5 files changed, 153 insertions(+), 81 deletions(-) diff --git a/components/infra-proxy-service/config/service.go b/components/infra-proxy-service/config/service.go index b450eed6d29..96e21d7de77 100644 --- a/components/infra-proxy-service/config/service.go +++ b/components/infra-proxy-service/config/service.go @@ -1,8 +1,21 @@ package config import ( + "fmt" + "net/url" + "os" + + "github.com/pkg/errors" + "github.com/spf13/viper" + + secrets "github.com/chef/automate/api/external/secrets" + "github.com/chef/automate/api/interservice/authz" + "github.com/chef/automate/components/infra-proxy-service/service" + "github.com/chef/automate/components/infra-proxy-service/storage/postgres/migration" + "github.com/chef/automate/lib/grpc/secureconn" + "github.com/chef/automate/lib/logger" + platform_config "github.com/chef/automate/lib/platform/config" "github.com/chef/automate/lib/tls/certs" - log "github.com/sirupsen/logrus" ) // Service config options @@ -18,30 +31,99 @@ type Service struct { SecretsAddress string `mapstructure:"secrets-address"` } -// Default returns a blank Service instance with default parameters -func Default() *Service { - return &Service{ - GRPC: "127.0.0.1:10153", - LogLevel: "info", - MigrationsPath: "/src/components/infra-proxy-service/storage/postgres/schema/sql/", +// ConfigFromViper returns a Service instance from the current viper config +func ConfigFromViper(configFile string) (*service.Service, error) { + // Set the file name of the configurations file + viper.SetConfigName("config") + + // Set the configuration file type + viper.SetConfigType("yaml") + + // Set the path to look for the configurations file + viper.AddConfigPath("../dev") + + if err := viper.ReadInConfig(); err != nil { + fail(errors.Wrap(err, `Could not read config file. Please pass a config file as the only argument to this command.`)) } -} -// SetLogLevel sets the log level for the service -func (s *Service) SetLogLevel() { - if s.LogLevel == "" { - return + cfg := Service{} + + if err := viper.Unmarshal(&cfg); err != nil { + fail(errors.Wrap(err, "couldn't parse configuration file")) + } + + if cfg.PGURL == "" { + var err error + cfg.PGURL, err = platform_config.PGURIFromEnvironment(cfg.Database) + if err != nil { + fail(errors.Wrap(err, "Failed to get pg uri")) + } + } + + l, err := logger.NewLogger(cfg.LogFormat, cfg.LogLevel) + if err != nil { + fail(errors.Wrap(err, "couldn't initialize logger")) + } + + cfg.FixupRelativeTLSPaths(configFile) + serviceCerts, err := cfg.ReadCerts() + if err != nil { + fail(errors.Wrap(err, "Could not read certs")) + } + connFactory := secureconn.NewFactory(*serviceCerts) + + mustBeADirectory(cfg.MigrationsPath) + u, err := url.Parse(cfg.PGURL) + if err != nil { + fail(errors.Wrapf(err, "could not parse pg_url %s from config", cfg.PGURL)) + } + migrationConfig := migration.Config{ + Path: cfg.MigrationsPath, + PGURL: u, + Logger: l, + } + + if cfg.AuthzAddress == "" { + fail(errors.New("missing required config authz_address")) + } + authzConn, err := connFactory.Dial("authz-service", cfg.AuthzAddress) + if err != nil { + fail(errors.Wrapf(err, "failed to dial authz-service at (%s)", cfg.AuthzAddress)) + } + authzClient := authz.NewAuthorizationServiceClient(authzConn) + + if cfg.SecretsAddress == "" { + fail(errors.New("missing required config secrets_address")) + } + secretsConn, err := connFactory.Dial("secrets-service", cfg.SecretsAddress) + if err != nil { + fail(errors.Wrapf(err, "failed to dial secrets-service at (%s)", cfg.SecretsAddress)) } - log.WithFields(log.Fields{ - "level": s.LogLevel, - }).Info("Setting log level") + // gets secrets client + secretsClient := secrets.NewSecretsServiceClient(secretsConn) - level, err := log.ParseLevel(s.LogLevel) + service, err := service.Start(l, migrationConfig, connFactory, secretsClient, authzClient) if err != nil { - log.WithField("level", s.LogLevel).WithError(err).Error("Using default level 'info'") - return + fail(errors.Wrap(err, "could not initialize storage")) } - log.SetLevel(level) + return service, nil +} + +// fail outputs the error and exits with a non-zero code +func fail(err error) { + // no error check: if this goes wrong, we're in trouble anyways + fmt.Fprint(os.Stderr, err.Error()) // nolint: gas + os.Exit(1) +} + +func mustBeADirectory(path string) { + stat, err := os.Stat(path) + if err == nil && stat.IsDir() { + return // everything's in its right place + } else if err != nil { + fail(errors.Wrapf(err, "open path %#v", path)) + } + fail(fmt.Errorf("path %#v is not a directory", path)) } diff --git a/components/infra-proxy-service/dev/config.yml b/components/infra-proxy-service/dev/config.yml index 8f60479ec10..b2c9206ad29 100644 --- a/components/infra-proxy-service/dev/config.yml +++ b/components/infra-proxy-service/dev/config.yml @@ -10,6 +10,6 @@ tls: pg_url: "postgresql://postgres@127.0.0.1:5432/infra_proxy_test?sslmode=disable" database: infra_proxy_test -migrations-path: storage/postgres/migration/sql/ +migrations-path: /src/components/infra-proxy-service/storage/postgres/migration/sql/ authz-address: "0.0.0.0:10130" secrets-address: "0.0.0.0:10131" diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go index 301b54a37d1..812880f24c4 100644 --- a/components/infra-proxy-service/integration_test/clients_test.go +++ b/components/infra-proxy-service/integration_test/clients_test.go @@ -20,4 +20,4 @@ func TestClientsReturnsEmptyList(t *testing.T) { fmt.Print(expected) fmt.Print(res) fmt.Print(err) -} \ No newline at end of file +} diff --git a/components/infra-proxy-service/integration_test/global_test.go b/components/infra-proxy-service/integration_test/global_test.go index 22f0977ff22..550ec9612f0 100644 --- a/components/infra-proxy-service/integration_test/global_test.go +++ b/components/infra-proxy-service/integration_test/global_test.go @@ -2,63 +2,30 @@ package integration_test import ( "os" - - "github.com/pkg/errors" - - "github.com/chef/automate/components/infra-proxy-service/config" - "github.com/chef/automate/components/infra-proxy-service/server" - "github.com/chef/automate/components/infra-proxy-service/service" - platform_config "github.com/chef/automate/lib/platform/config" + "testing" ) // Global variables var ( + cFile = "/src/components/infra-proxy-service/dev/config" // This suite variable will be available for every single test as long as they // belong to the 'integration_test' package. suite = NewSuite() ) -// GlobalSetup makes backend connections to postgres. It also sets -// global vars to usable values. -func (s *Suite) GlobalSetup() error { - // set global infraProxy - var err error - infraProxy, err = newInfraProxyServer() - - if err != nil { - return err - } - - return nil -} - +func TestMain(m *testing.M) { + // Global Setup hook: Here is where you can initialize anythings you need + // for your tests to run, things like; Initialize ES indices, insert + // nodes or runs, etc. + suite.GlobalSetup() -// newInfraProxyServer initializes a InfraProxyServer with the default config -func newInfraProxyServer() (*server.Server, error) { - _, haveSvcName := os.LookupEnv(A2_SVC_NAME) - _, haveSvcPath := os.LookupEnv(A2_SVC_PATH) - if !(haveSvcName && haveSvcPath) { - _ = os.Setenv(A2_SVC_NAME, defaultA2ServiceName) - _ = os.Setenv(A2_SVC_PATH, defaultA2ServicePath) - } + // Execute the test suite and record the exit code + exitCode := m.Run() - uri, err := platform_config.PGURIFromEnvironment(pgDatabaseName) + // Teardown hook: It says it all, this hook should clean documents + // from ES so that the next test can run on a clean env. + suite.GlobalTeardown() - if err != nil { - return nil, errors.Wrap(err, "Failed to get pg uri from environment variables") - } - - cfg := config.Default() - cfg.PGURL = uri - cfg.Database = pgDatabaseName - - service, err := service.Start(cfg.LogLevel, migrationConfig, nil, nil, nil) - if err != nil { - fail(errors.Wrap(err, "could not initialize storage")) - } - - fail(server.GRPC(cfg.GRPC, service)) - - srv := server.NewServer(cfg) - return srv, nil -} \ No newline at end of file + // call with result of m.Run() + os.Exit(exitCode) +} diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index ec7ec1fb91f..896aff23f96 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -1,12 +1,12 @@ package integration_test import ( + "github.com/chef/automate/components/infra-proxy-service/config" "github.com/chef/automate/components/infra-proxy-service/server" ) // Global variables var ( - // A global Infra Proxy Server instance to call any rpc function // // From any test you can directly call: @@ -16,18 +16,41 @@ var ( infraProxy *server.Server ) -const ( - pgDatabaseName = "chef_infra_proxy" - A2_SVC_NAME = "A2_SVC_NAME" - A2_SVC_PATH = "A2_SVC_PATH" - defaultA2ServiceName = "infra-proxy-service" - defaultA2ServicePath = "/hab/svc/infra-proxy-service" -) - - -type Suite struct {} +type Suite struct{} // Just returns a new struct. You have to call GlobalSetup() to setup func NewSuite() *Suite { return new(Suite) -} \ No newline at end of file +} + +// GlobalSetup makes all connections. +func (s *Suite) GlobalSetup() error { + var err error + // set global infraProxy + infraProxy, err = newInfraProxyServer() + + if err != nil { + return err + } + + return nil +} + +// GlobalTeardown is the place where you tear everything down after we have finished +func (s *Suite) GlobalTeardown() {} + +// newInfraProxyServer initializes a InfraProxyServer with the default config +func newInfraProxyServer() (*server.Server, error) { + service, err := config.ConfigFromViper(cFile) + if err != nil { + return nil, err + } + + gRPC := server.NewServer(service) + err = server.GRPC("127.0.0.1:10153", service) + if err != nil { + return nil, err + } + + return gRPC, nil +} From c0331466a3274d3acfcba806c5e67b64aaf0bb2c Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 8 Feb 2021 14:12:38 +0530 Subject: [PATCH 05/16] Required global setup Signed-off-by: Vivek Singh --- .../infra-proxy-service/config/service.go | 10 ++++---- components/infra-proxy-service/dev/config.yml | 2 +- .../integration_test/clients_test.go | 23 ------------------ .../integration_test/servers_test.go | 24 +++++++++++++++++++ .../infra-proxy-service/server/clients.go | 1 + 5 files changed, 30 insertions(+), 30 deletions(-) delete mode 100644 components/infra-proxy-service/integration_test/clients_test.go create mode 100644 components/infra-proxy-service/integration_test/servers_test.go diff --git a/components/infra-proxy-service/config/service.go b/components/infra-proxy-service/config/service.go index 96e21d7de77..26c694eeced 100644 --- a/components/infra-proxy-service/config/service.go +++ b/components/infra-proxy-service/config/service.go @@ -52,13 +52,11 @@ func ConfigFromViper(configFile string) (*service.Service, error) { fail(errors.Wrap(err, "couldn't parse configuration file")) } - if cfg.PGURL == "" { - var err error - cfg.PGURL, err = platform_config.PGURIFromEnvironment(cfg.Database) - if err != nil { - fail(errors.Wrap(err, "Failed to get pg uri")) - } + pgURL, err := platform_config.PGURIFromEnvironment(cfg.Database) + if err != nil { + fail(errors.Wrap(err, "Failed to get pg uri")) } + cfg.PGURL = pgURL l, err := logger.NewLogger(cfg.LogFormat, cfg.LogLevel) if err != nil { diff --git a/components/infra-proxy-service/dev/config.yml b/components/infra-proxy-service/dev/config.yml index b2c9206ad29..4234b533ac8 100644 --- a/components/infra-proxy-service/dev/config.yml +++ b/components/infra-proxy-service/dev/config.yml @@ -9,7 +9,7 @@ tls: root_ca_path: ../../../dev/certs/Chef_Automate_FAKE_Dev.crt pg_url: "postgresql://postgres@127.0.0.1:5432/infra_proxy_test?sslmode=disable" -database: infra_proxy_test +database: chef_infra_proxy migrations-path: /src/components/infra-proxy-service/storage/postgres/migration/sql/ authz-address: "0.0.0.0:10130" secrets-address: "0.0.0.0:10131" diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go deleted file mode 100644 index 812880f24c4..00000000000 --- a/components/infra-proxy-service/integration_test/clients_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package integration_test - -import ( - "context" - "fmt" - "testing" - - "github.com/chef/automate/api/interservice/infra_proxy/request" - "github.com/chef/automate/api/interservice/infra_proxy/response" -) - - -func TestClientsReturnsEmptyList(t *testing.T) { - // rpc GetClients (request.Clients) returns (response.Clients) - ctx := context.Background() - req := request.Clients{} - - expected := new(response.Clients) - res, err := infraProxy.GetClients(ctx, &req) - fmt.Print(expected) - fmt.Print(res) - fmt.Print(err) -} diff --git a/components/infra-proxy-service/integration_test/servers_test.go b/components/infra-proxy-service/integration_test/servers_test.go new file mode 100644 index 00000000000..b735a1f56af --- /dev/null +++ b/components/infra-proxy-service/integration_test/servers_test.go @@ -0,0 +1,24 @@ +package integration_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/chef/automate/api/interservice/infra_proxy/request" + "github.com/chef/automate/api/interservice/infra_proxy/response" +) + +func TestGetServersReturnsEmptyList(t *testing.T) { + // rpc GetGetServers (request.GetServers) returns (response.GetServers) + ctx := context.Background() + req := &request.GetServers{} + + expected := new(response.GetServers) + res, err := infraProxy.GetServers(ctx, req) + assert.NoError(t, err) + assert.NotNil(t, res) + + assert.NotNil(t, expected) +} diff --git a/components/infra-proxy-service/server/clients.go b/components/infra-proxy-service/server/clients.go index 4dd97475010..d4a7a872501 100644 --- a/components/infra-proxy-service/server/clients.go +++ b/components/infra-proxy-service/server/clients.go @@ -13,6 +13,7 @@ import ( // GetClients get clients list func (s *Server) GetClients(ctx context.Context, req *request.Clients) (*response.Clients, error) { c, err := s.createClient(ctx, req.OrgId, req.ServerId) + if err != nil { return nil, err } From b71d31323b98b8bc10a1f1f42a05a6c62ff19fd8 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 8 Feb 2021 15:17:51 +0530 Subject: [PATCH 06/16] GetServers test running Signed-off-by: Vivek Singh --- .../infra-proxy-service/integration_test/servers_test.go | 3 +-- components/infra-proxy-service/integration_test/suite_test.go | 4 ---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/components/infra-proxy-service/integration_test/servers_test.go b/components/infra-proxy-service/integration_test/servers_test.go index b735a1f56af..401343ca100 100644 --- a/components/infra-proxy-service/integration_test/servers_test.go +++ b/components/infra-proxy-service/integration_test/servers_test.go @@ -19,6 +19,5 @@ func TestGetServersReturnsEmptyList(t *testing.T) { res, err := infraProxy.GetServers(ctx, req) assert.NoError(t, err) assert.NotNil(t, res) - - assert.NotNil(t, expected) + assert.Equal(t, expected, res) } diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index 896aff23f96..d4c34a931f1 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -47,10 +47,6 @@ func newInfraProxyServer() (*server.Server, error) { } gRPC := server.NewServer(service) - err = server.GRPC("127.0.0.1:10153", service) - if err != nil { - return nil, err - } return gRPC, nil } From 8775e095e1b40f50058913f67c56eaa405e197c2 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 8 Feb 2021 18:54:32 +0530 Subject: [PATCH 07/16] spin up local chef server dev Signed-off-by: Vivek Singh --- .../integration_test/node_attribute_test.go | 2 +- .../infra-proxy-service/config/service.go | 3 - components/infra-proxy-service/dev/config.yml | 6 +- .../integration_test/clients_test.go | 27 +++++++ .../integration_test/servers_test.go | 23 ------ .../integration_test/suite_test.go | 74 +++++++++++++++++++ 6 files changed, 105 insertions(+), 30 deletions(-) create mode 100644 components/infra-proxy-service/integration_test/clients_test.go delete mode 100644 components/infra-proxy-service/integration_test/servers_test.go diff --git a/components/config-mgmt-service/integration_test/node_attribute_test.go b/components/config-mgmt-service/integration_test/node_attribute_test.go index bf878b2f4af..e74873fe888 100644 --- a/components/config-mgmt-service/integration_test/node_attribute_test.go +++ b/components/config-mgmt-service/integration_test/node_attribute_test.go @@ -248,7 +248,7 @@ func TestNodeAttributeProjectFilter(t *testing.T) { }, { description: "Node has no projects; request unassigned projects allowed", - ctx: contextWithProjects([]string{authzConstants.UnassignedProjectID}), + ctx: ([contextWithProjects]string{authzConstants.UnassignedProjectID}), nodeProjects: []string{}, expected: expectedSuccess, }, diff --git a/components/infra-proxy-service/config/service.go b/components/infra-proxy-service/config/service.go index 26c694eeced..7040f7d09be 100644 --- a/components/infra-proxy-service/config/service.go +++ b/components/infra-proxy-service/config/service.go @@ -35,10 +35,8 @@ type Service struct { func ConfigFromViper(configFile string) (*service.Service, error) { // Set the file name of the configurations file viper.SetConfigName("config") - // Set the configuration file type viper.SetConfigType("yaml") - // Set the path to look for the configurations file viper.AddConfigPath("../dev") @@ -47,7 +45,6 @@ func ConfigFromViper(configFile string) (*service.Service, error) { } cfg := Service{} - if err := viper.Unmarshal(&cfg); err != nil { fail(errors.Wrap(err, "couldn't parse configuration file")) } diff --git a/components/infra-proxy-service/dev/config.yml b/components/infra-proxy-service/dev/config.yml index 4234b533ac8..8e89b4bb4ba 100644 --- a/components/infra-proxy-service/dev/config.yml +++ b/components/infra-proxy-service/dev/config.yml @@ -4,9 +4,9 @@ log-level: "info" log-format: "text" tls: - cert_path: ../../../dev/certs/infra-proxy-service.crt - key_path: ../../../dev/certs/infra-proxy-service.key - root_ca_path: ../../../dev/certs/Chef_Automate_FAKE_Dev.crt + cert_path: /hab/svc/infra-proxy-service/config/service.crt + key_path: /hab/svc/infra-proxy-service/config/service.key + root_ca_path: /hab/svc/infra-proxy-service/config/root_ca.crt pg_url: "postgresql://postgres@127.0.0.1:5432/infra_proxy_test?sslmode=disable" database: chef_infra_proxy diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go new file mode 100644 index 00000000000..ce8ccd27e7b --- /dev/null +++ b/components/infra-proxy-service/integration_test/clients_test.go @@ -0,0 +1,27 @@ +package integration_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/chef/automate/api/interservice/infra_proxy/request" +) + +func TestClientsReturnsEmptyList(t *testing.T) { + // rpc GetGetServers (request.Clients) returns (response.Clients) + ctx := context.Background() + err := suite.SetUpAutoDeployChefServer() + req := &request.Clients{ + ServerId: "auto-deployed-test-server", + OrgId: "auto-deployed-test-org", + } + + assert.NoError(t, err) + + res, err := infraProxy.GetClients(ctx, req) + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, 0, len(res.GetClients())) +} diff --git a/components/infra-proxy-service/integration_test/servers_test.go b/components/infra-proxy-service/integration_test/servers_test.go deleted file mode 100644 index 401343ca100..00000000000 --- a/components/infra-proxy-service/integration_test/servers_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package integration_test - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/chef/automate/api/interservice/infra_proxy/request" - "github.com/chef/automate/api/interservice/infra_proxy/response" -) - -func TestGetServersReturnsEmptyList(t *testing.T) { - // rpc GetGetServers (request.GetServers) returns (response.GetServers) - ctx := context.Background() - req := &request.GetServers{} - - expected := new(response.GetServers) - res, err := infraProxy.GetServers(ctx, req) - assert.NoError(t, err) - assert.NotNil(t, res) - assert.Equal(t, expected, res) -} diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index d4c34a931f1..0ad6d0c0a2c 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -1,7 +1,14 @@ package integration_test import ( + "context" + "errors" + "io/ioutil" + "os" + + "github.com/chef/automate/api/interservice/infra_proxy/request" "github.com/chef/automate/components/infra-proxy-service/config" + "github.com/chef/automate/components/infra-proxy-service/constants" "github.com/chef/automate/components/infra-proxy-service/server" ) @@ -39,6 +46,49 @@ func (s *Suite) GlobalSetup() error { // GlobalTeardown is the place where you tear everything down after we have finished func (s *Suite) GlobalTeardown() {} +func (s *Suite) SetUpAutoDeployChefServer() error { + ctx := context.Background() + var serverId string + server, _ := infraProxy.GetServer(ctx, &request.GetServer{ + Id: "auto-deployed-test-server", + }) + + serverId = server.Server.Id + + // Ignore insertion if already exists + if server == nil { + + server, err := infraProxy.CreateServer(ctx, &request.CreateServer{ + Id: "auto-deployed-test-server", + Name: "auto-deployed-test-server", + IpAddress: "127.0.0.1", + Fqdn: defaultServerHost(), + }) + + if err != nil { + return err + } + + serverId = server.Server.Id + } + + pemFile, err := defaultServerAdminKey() + if err != nil { + return errors.New("newerror") + } + + _, err = infraProxy.CreateOrg(ctx, &request.CreateOrg{ + Id: "auto-deployed-test-org", + ServerId: serverId, + Name: "test", + AdminUser: "pivotal", + AdminKey: pemFile, + Projects: []string{constants.UnassignedProjectID}, + }) + + return err +} + // newInfraProxyServer initializes a InfraProxyServer with the default config func newInfraProxyServer() (*server.Server, error) { service, err := config.ConfigFromViper(cFile) @@ -50,3 +100,27 @@ func newInfraProxyServer() (*server.Server, error) { return gRPC, nil } + +func defaultServerHost() string { + cName := os.Getenv("CONTAINER_HOSTNAME") + serverHost := "a2-dev.test" + if cName != "" { + serverHost = cName + serverHost + } + + return serverHost +} + +func defaultServerAdminKey() (string, error) { + file, err := os.Open("/hab/svc/automate-cs-oc-erchef/data/pivotal.pem") + if err != nil { + return "", err + } + + content, err := ioutil.ReadAll(file) + if err != nil { + return "", err + } + + return string(content), nil +} From d58c8a7009b140e407a000e0ab7f11c1a4b1c150 Mon Sep 17 00:00:00 2001 From: Kallol Roy Date: Tue, 9 Feb 2021 10:23:32 +0530 Subject: [PATCH 08/16] Changes to create auth_token while sending GRPC requests Signed-off-by: Kallol Roy --- .../integration_test/clients_test.go | 16 +++++++++------- .../integration_test/suite_test.go | 13 +++++++------ components/infra-proxy-service/server/orgs.go | 5 ++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go index ce8ccd27e7b..cf013f4fa76 100644 --- a/components/infra-proxy-service/integration_test/clients_test.go +++ b/components/infra-proxy-service/integration_test/clients_test.go @@ -2,24 +2,26 @@ package integration_test import ( "context" + "fmt" "testing" - - "github.com/stretchr/testify/assert" + "time" "github.com/chef/automate/api/interservice/infra_proxy/request" + "github.com/stretchr/testify/assert" ) func TestClientsReturnsEmptyList(t *testing.T) { // rpc GetGetServers (request.Clients) returns (response.Clients) + + testTimeStamp := time.Now().Second() + err := suite.SetUpAutoDeployChefServer(testTimeStamp) + assert.NoError(t, err) + ctx := context.Background() - err := suite.SetUpAutoDeployChefServer() req := &request.Clients{ ServerId: "auto-deployed-test-server", - OrgId: "auto-deployed-test-org", + OrgId: fmt.Sprintf("auto-deployed-test-org-%d", testTimeStamp), } - - assert.NoError(t, err) - res, err := infraProxy.GetClients(ctx, req) assert.NoError(t, err) assert.NotNil(t, res) diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index 0ad6d0c0a2c..f303ce84741 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -3,6 +3,7 @@ package integration_test import ( "context" "errors" + "fmt" "io/ioutil" "os" @@ -46,19 +47,17 @@ func (s *Suite) GlobalSetup() error { // GlobalTeardown is the place where you tear everything down after we have finished func (s *Suite) GlobalTeardown() {} -func (s *Suite) SetUpAutoDeployChefServer() error { +func (s *Suite) SetUpAutoDeployChefServer(testTimeStamp int) error { ctx := context.Background() var serverId string server, _ := infraProxy.GetServer(ctx, &request.GetServer{ Id: "auto-deployed-test-server", }) - serverId = server.Server.Id - // Ignore insertion if already exists if server == nil { - server, err := infraProxy.CreateServer(ctx, &request.CreateServer{ + createServerResponse, err := infraProxy.CreateServer(ctx, &request.CreateServer{ Id: "auto-deployed-test-server", Name: "auto-deployed-test-server", IpAddress: "127.0.0.1", @@ -69,6 +68,8 @@ func (s *Suite) SetUpAutoDeployChefServer() error { return err } + serverId = createServerResponse.Server.Id + } else { serverId = server.Server.Id } @@ -78,9 +79,9 @@ func (s *Suite) SetUpAutoDeployChefServer() error { } _, err = infraProxy.CreateOrg(ctx, &request.CreateOrg{ - Id: "auto-deployed-test-org", + Id: fmt.Sprintf("auto-deployed-test-org-%d", testTimeStamp), ServerId: serverId, - Name: "test", + Name: fmt.Sprintf("test-%d", testTimeStamp), AdminUser: "pivotal", AdminKey: pemFile, Projects: []string{constants.UnassignedProjectID}, diff --git a/components/infra-proxy-service/server/orgs.go b/components/infra-proxy-service/server/orgs.go index 8606d70f25b..de5ad954d42 100644 --- a/components/infra-proxy-service/server/orgs.go +++ b/components/infra-proxy-service/server/orgs.go @@ -10,6 +10,7 @@ import ( "github.com/chef/automate/components/infra-proxy-service/service" "github.com/chef/automate/components/infra-proxy-service/storage" "github.com/chef/automate/components/infra-proxy-service/validation" + "github.com/chef/automate/lib/grpc/auth_context" ) // CreateOrg creates a new org @@ -41,7 +42,9 @@ func (s *Server) CreateOrg(ctx context.Context, req *request.CreateOrg) (*respon return nil, err } - org, err := s.service.Storage.StoreOrg(ctx, req.Id, req.Name, req.AdminUser, secretID.GetId(), req.ServerId, req.Projects) + auth_cntx := auth_context.NewContext(ctx, + []string{"tls:service:deployment-service:internal"}, req.Projects, "res", "act") + org, err := s.service.Storage.StoreOrg(auth_cntx, req.Id, req.Name, req.AdminUser, secretID.GetId(), req.ServerId, req.Projects) if err != nil { return nil, service.ParseStorageError(err, *req, "org") } From 73a6e2189aa40372a2a7c8151f54b3a3745c18fc Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 9 Feb 2021 15:08:52 +0530 Subject: [PATCH 09/16] Added local deployed chef server and its setup Signed-off-by: Vivek Singh --- .studio/infra-proxy-service | 7 ++ .../integration_test/clients_test.go | 31 +++++--- .../integration_test/global_test.go | 4 +- .../integration_test/suite_test.go | 75 ------------------- components/infra-proxy-service/server/orgs.go | 5 +- .../infra/chef-repo/clients/chef-load-1.json | 8 ++ .../infra/chef-repo/clients/chef-load-2.json | 7 ++ 7 files changed, 45 insertions(+), 92 deletions(-) create mode 100644 dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json create mode 100644 dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json diff --git a/.studio/infra-proxy-service b/.studio/infra-proxy-service index 7efab6a591b..52a04be92b9 100644 --- a/.studio/infra-proxy-service +++ b/.studio/infra-proxy-service @@ -16,6 +16,9 @@ document "infra_service_integration" < /dev/null; then diff --git a/components/infra-proxy-service/integration_test/clients_test.go b/components/infra-proxy-service/integration_test/clients_test.go index cf013f4fa76..767bb5d8100 100644 --- a/components/infra-proxy-service/integration_test/clients_test.go +++ b/components/infra-proxy-service/integration_test/clients_test.go @@ -2,28 +2,35 @@ package integration_test import ( "context" - "fmt" "testing" - "time" "github.com/chef/automate/api/interservice/infra_proxy/request" "github.com/stretchr/testify/assert" ) -func TestClientsReturnsEmptyList(t *testing.T) { - // rpc GetGetServers (request.Clients) returns (response.Clients) - - testTimeStamp := time.Now().Second() - err := suite.SetUpAutoDeployChefServer(testTimeStamp) - assert.NoError(t, err) - +func TestGetClients(t *testing.T) { + // rpc GetClients (request.Clients) returns (response.Clients) ctx := context.Background() req := &request.Clients{ - ServerId: "auto-deployed-test-server", - OrgId: fmt.Sprintf("auto-deployed-test-org-%d", testTimeStamp), + ServerId: autoDeployedChefServerID, + OrgId: autoDeployedChefOrganizationID, } res, err := infraProxy.GetClients(ctx, req) assert.NoError(t, err) assert.NotNil(t, res) - assert.Equal(t, 0, len(res.GetClients())) + assert.Equal(t, 3, len(res.GetClients())) +} + +func TestGetClient(t *testing.T) { + // rpc GetClient (request.Client) returns (response.Client) + ctx := context.Background() + req := &request.Client{ + ServerId: autoDeployedChefServerID, + OrgId: autoDeployedChefOrganizationID, + Name: "chef-load-1", + } + res, err := infraProxy.GetClient(ctx, req) + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, "chef-load-1", res.Name) } diff --git a/components/infra-proxy-service/integration_test/global_test.go b/components/infra-proxy-service/integration_test/global_test.go index 550ec9612f0..d81ca36a4ad 100644 --- a/components/infra-proxy-service/integration_test/global_test.go +++ b/components/infra-proxy-service/integration_test/global_test.go @@ -7,7 +7,9 @@ import ( // Global variables var ( - cFile = "/src/components/infra-proxy-service/dev/config" + autoDeployedChefServerID = "local-dev" + autoDeployedChefOrganizationID = "test-org" + cFile = "/src/components/infra-proxy-service/dev/config" // This suite variable will be available for every single test as long as they // belong to the 'integration_test' package. suite = NewSuite() diff --git a/components/infra-proxy-service/integration_test/suite_test.go b/components/infra-proxy-service/integration_test/suite_test.go index f303ce84741..d4c34a931f1 100644 --- a/components/infra-proxy-service/integration_test/suite_test.go +++ b/components/infra-proxy-service/integration_test/suite_test.go @@ -1,15 +1,7 @@ package integration_test import ( - "context" - "errors" - "fmt" - "io/ioutil" - "os" - - "github.com/chef/automate/api/interservice/infra_proxy/request" "github.com/chef/automate/components/infra-proxy-service/config" - "github.com/chef/automate/components/infra-proxy-service/constants" "github.com/chef/automate/components/infra-proxy-service/server" ) @@ -47,49 +39,6 @@ func (s *Suite) GlobalSetup() error { // GlobalTeardown is the place where you tear everything down after we have finished func (s *Suite) GlobalTeardown() {} -func (s *Suite) SetUpAutoDeployChefServer(testTimeStamp int) error { - ctx := context.Background() - var serverId string - server, _ := infraProxy.GetServer(ctx, &request.GetServer{ - Id: "auto-deployed-test-server", - }) - - // Ignore insertion if already exists - if server == nil { - - createServerResponse, err := infraProxy.CreateServer(ctx, &request.CreateServer{ - Id: "auto-deployed-test-server", - Name: "auto-deployed-test-server", - IpAddress: "127.0.0.1", - Fqdn: defaultServerHost(), - }) - - if err != nil { - return err - } - - serverId = createServerResponse.Server.Id - } else { - serverId = server.Server.Id - } - - pemFile, err := defaultServerAdminKey() - if err != nil { - return errors.New("newerror") - } - - _, err = infraProxy.CreateOrg(ctx, &request.CreateOrg{ - Id: fmt.Sprintf("auto-deployed-test-org-%d", testTimeStamp), - ServerId: serverId, - Name: fmt.Sprintf("test-%d", testTimeStamp), - AdminUser: "pivotal", - AdminKey: pemFile, - Projects: []string{constants.UnassignedProjectID}, - }) - - return err -} - // newInfraProxyServer initializes a InfraProxyServer with the default config func newInfraProxyServer() (*server.Server, error) { service, err := config.ConfigFromViper(cFile) @@ -101,27 +50,3 @@ func newInfraProxyServer() (*server.Server, error) { return gRPC, nil } - -func defaultServerHost() string { - cName := os.Getenv("CONTAINER_HOSTNAME") - serverHost := "a2-dev.test" - if cName != "" { - serverHost = cName + serverHost - } - - return serverHost -} - -func defaultServerAdminKey() (string, error) { - file, err := os.Open("/hab/svc/automate-cs-oc-erchef/data/pivotal.pem") - if err != nil { - return "", err - } - - content, err := ioutil.ReadAll(file) - if err != nil { - return "", err - } - - return string(content), nil -} diff --git a/components/infra-proxy-service/server/orgs.go b/components/infra-proxy-service/server/orgs.go index de5ad954d42..8606d70f25b 100644 --- a/components/infra-proxy-service/server/orgs.go +++ b/components/infra-proxy-service/server/orgs.go @@ -10,7 +10,6 @@ import ( "github.com/chef/automate/components/infra-proxy-service/service" "github.com/chef/automate/components/infra-proxy-service/storage" "github.com/chef/automate/components/infra-proxy-service/validation" - "github.com/chef/automate/lib/grpc/auth_context" ) // CreateOrg creates a new org @@ -42,9 +41,7 @@ func (s *Server) CreateOrg(ctx context.Context, req *request.CreateOrg) (*respon return nil, err } - auth_cntx := auth_context.NewContext(ctx, - []string{"tls:service:deployment-service:internal"}, req.Projects, "res", "act") - org, err := s.service.Storage.StoreOrg(auth_cntx, req.Id, req.Name, req.AdminUser, secretID.GetId(), req.ServerId, req.Projects) + org, err := s.service.Storage.StoreOrg(ctx, req.Id, req.Name, req.AdminUser, secretID.GetId(), req.ServerId, req.Projects) if err != nil { return nil, service.ParseStorageError(err, *req, "org") } diff --git a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json new file mode 100644 index 00000000000..788f5d46a40 --- /dev/null +++ b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json @@ -0,0 +1,8 @@ +{ + "name": "chef-load-1", + "validator": false, + "admin": false, + "chef_type": "client", + "create_key": false + } + \ No newline at end of file diff --git a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json new file mode 100644 index 00000000000..0123bcb6ddf --- /dev/null +++ b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json @@ -0,0 +1,7 @@ +{ + "name": "chef-load-2", + "validator": false, + "admin": false, + "chef_type": "client", + "create_key": false + } \ No newline at end of file From f116bb4db752c7da723c87def6576c3ebf3378cd Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 9 Feb 2021 15:17:14 +0530 Subject: [PATCH 10/16] fixes end of line issues Signed-off-by: Vivek Singh --- dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json | 3 +-- dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json index 788f5d46a40..c2ae2a8b327 100644 --- a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json +++ b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-1.json @@ -4,5 +4,4 @@ "admin": false, "chef_type": "client", "create_key": false - } - \ No newline at end of file +} diff --git a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json index 0123bcb6ddf..0f7bb638212 100644 --- a/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json +++ b/dev-docs/adding-data/infra/chef-repo/clients/chef-load-2.json @@ -4,4 +4,4 @@ "admin": false, "chef_type": "client", "create_key": false - } \ No newline at end of file +} From d00375717f80836e81f3d6be916ac9ea43720343 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 9 Feb 2021 18:15:40 +0530 Subject: [PATCH 11/16] remove automate gateway integration test cases Signed-off-by: Vivek Singh --- .../integration/infra_proxy_test.go | 79 ------------------- .../integration/suite_test.go | 18 ++--- .../integration_test/node_attribute_test.go | 2 +- 3 files changed, 9 insertions(+), 90 deletions(-) delete mode 100644 components/automate-gateway/integration/infra_proxy_test.go diff --git a/components/automate-gateway/integration/infra_proxy_test.go b/components/automate-gateway/integration/infra_proxy_test.go deleted file mode 100644 index fa1c1196a82..00000000000 --- a/components/automate-gateway/integration/infra_proxy_test.go +++ /dev/null @@ -1,79 +0,0 @@ -package integration - -import ( - "bytes" - "crypto/tls" - "encoding/json" - "fmt" - "io" - "net/http" - "time" - - "github.com/stretchr/testify/assert" -) - -type parsedResponse map[string]interface{} - -var client = NewClient() -var currenTimeStamp = time.Now().Second() - -func NewClient() *http.Client { - transport := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - return &http.Client{Transport: transport} -} - -func (suite *GatewayTestSuite) TestInfraProxyServersAPI() { - // add a mock server to Chef Infra - addServer(suite) - // check if the added server is available - getServers(suite) -} - -func addServer(suite *GatewayTestSuite) { - ipAddress := "128.170.178.154" - postBody, _ := json.Marshal(map[string]string{ - "id": fmt.Sprintf("chef-server-%d-id", currenTimeStamp), - "name": fmt.Sprintf("chef-server-%d", currenTimeStamp), - "fqdn": fmt.Sprintf("chef-server-%d.com", currenTimeStamp), - "ip_address": ipAddress, - }) - post, err := http.NewRequest("POST", "https://127.0.0.1/api/v0/infra/servers", bytes.NewBuffer(postBody)) - post.Header.Add("api-token", suite.automateAPIToken) - response, err := client.Do(post) - if assert.Nil(suite.T(), err, "Error sending request %v", err) { - assert.True(suite.T(), response.StatusCode == 200, "Expected Get Servers status code %d, got: %d", 200, response.StatusCode) - } -} - -func getServers(suite *GatewayTestSuite) { - get, err := http.NewRequest("GET", "https://127.0.0.1/api/v0/infra/servers", nil) - get.Header.Add("api-token", suite.automateAPIToken) - response, err := client.Do(get) - if assert.Nil(suite.T(), err, "Error sending request %v", err) { - assert.True(suite.T(), response.StatusCode == 200, "Expected Get Servers status code %d, got: %d", 200, response.StatusCode) - } - resp, err := parseResponseBody(response.Body) - servers := resp["servers"].([]interface{}) - found := false - for _, s := range servers { - m := s.(map[string]interface{}) - if m["fqdn"].(string) == fmt.Sprintf("chef-server-%d.com", currenTimeStamp) { - found = true - break - } - } - - assert.True(suite.T(), found, "Expected to get server %s but failed to receive the details", fmt.Sprintf("chef-server-%d.com", currenTimeStamp)) -} - -func parseResponseBody(body io.ReadCloser) (parsedResponse, error) { - m := make(map[string]interface{}) - err := json.NewDecoder(body).Decode(&m) - if err != nil { - return nil, err - } - - return m, err -} diff --git a/components/automate-gateway/integration/suite_test.go b/components/automate-gateway/integration/suite_test.go index 86a33cf9568..64d99738540 100644 --- a/components/automate-gateway/integration/suite_test.go +++ b/components/automate-gateway/integration/suite_test.go @@ -20,11 +20,10 @@ const ( ) type GatewayTestSuite struct { - ctx context.Context - target *acceptanceTarget - clients gateway.ClientsFactory - gwConn *grpc.ClientConn - automateAPIToken string + ctx context.Context + target *acceptanceTarget + clients gateway.ClientsFactory + gwConn *grpc.ClientConn suite.Suite } @@ -59,11 +58,10 @@ func NewGatewayTestSuite(ctx context.Context, t *testing.T, target *acceptanceTa } return &GatewayTestSuite{ - ctx: ctx, - target: target, - clients: clients, - gwConn: gwConn, - automateAPIToken: os.Getenv("AUTOMATE_API_TOKEN"), + ctx: ctx, + target: target, + clients: clients, + gwConn: gwConn, }, nil } diff --git a/components/config-mgmt-service/integration_test/node_attribute_test.go b/components/config-mgmt-service/integration_test/node_attribute_test.go index e74873fe888..bf878b2f4af 100644 --- a/components/config-mgmt-service/integration_test/node_attribute_test.go +++ b/components/config-mgmt-service/integration_test/node_attribute_test.go @@ -248,7 +248,7 @@ func TestNodeAttributeProjectFilter(t *testing.T) { }, { description: "Node has no projects; request unassigned projects allowed", - ctx: ([contextWithProjects]string{authzConstants.UnassignedProjectID}), + ctx: contextWithProjects([]string{authzConstants.UnassignedProjectID}), nodeProjects: []string{}, expected: expectedSuccess, }, From e17ba8ad6d7c48bfc97edc05f4032741b186820e Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 9 Feb 2021 18:43:21 +0530 Subject: [PATCH 12/16] run server unit test cases only Signed-off-by: Vivek Singh --- components/infra-proxy-service/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/infra-proxy-service/Makefile b/components/infra-proxy-service/Makefile index 6dfae0070ca..95e6090925f 100644 --- a/components/infra-proxy-service/Makefile +++ b/components/infra-proxy-service/Makefile @@ -1,6 +1,6 @@ include ../../Makefile.common_go -PACKAGE_PATH = github.com/chef/automate/components/infra-proxy-service +PACKAGE_PATH = github.com/chef/automate/components/infra-proxy-service/server BINS = ${PACKAGE_PATH}/cmd/infra-proxy-service MIGRATION_READMES = storage/postgres/migration/sql/README.md From 1565ae8a4c8b414d1f70ceef7252de6ec806125e Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 10 Feb 2021 13:31:41 +0530 Subject: [PATCH 13/16] add integration test to ci Signed-off-by: Vivek Singh --- .expeditor/verify_private.pipeline.yml | 16 ++++++++++++++++ .studio/infra-proxy-service | 8 ++++---- .../infra-proxy-service/storage/postgres/orgs.go | 7 ++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.expeditor/verify_private.pipeline.yml b/.expeditor/verify_private.pipeline.yml index 78b1340697c..712166956b9 100644 --- a/.expeditor/verify_private.pipeline.yml +++ b/.expeditor/verify_private.pipeline.yml @@ -429,6 +429,22 @@ steps: - HAB_STUDIO_SUP=false - HAB_NONINTERACTIVE=true + - label: "infra-proxy-service" + command: + - . scripts/verify_setup.sh + - hab studio run "source scripts/verify_studio_init.sh && start_infra_proxy_service && infra_service_integration" + timeout_in_minutes: 20 + retry: + automatic: + limit: 1 + expeditor: + executor: + docker: + privileged: true + environment: + - HAB_STUDIO_SUP=false + - HAB_NONINTERACTIVE=true + # # The following tests all use the integration test framework for # end-to-end testing. These tests all test full deployments of diff --git a/.studio/infra-proxy-service b/.studio/infra-proxy-service index 52a04be92b9..b29b7473f0b 100644 --- a/.studio/infra-proxy-service +++ b/.studio/infra-proxy-service @@ -84,17 +84,17 @@ function infra_service_load_sample_data() { } function infra_service_create_servers_orgs() { - chef-automate dev grpcurl automate-gateway -- -d \ + chef-automate dev grpcurl infra-proxy-service -- -d \ "$(cat << EOF {"id": "$1", "name": "$2", "ip_address": "$3", "fqdn": "$4"} EOF - )" chef.automate.api.infra_proxy.InfraProxy.CreateServer >/dev/null + )" chef.automate.domain.infra_proxy.service.InfraProxyService.CreateServer >/dev/null - chef-automate dev grpcurl automate-gateway -- -d \ + chef-automate dev grpcurl infra-proxy-service -- -d \ "$(cat << EOF {"id": "$5", "name": "$6", "admin_user": "$7", "admin_key": "$8", "server_id": "$1", "projects": []} EOF - )" chef.automate.api.infra_proxy.InfraProxy.CreateOrg >/dev/null + )" chef.automate.domain.infra_proxy.service.InfraProxyService.CreateOrg >/dev/null } function infra_service_load_chef_repo() { diff --git a/components/infra-proxy-service/storage/postgres/orgs.go b/components/infra-proxy-service/storage/postgres/orgs.go index 8d2fc007a86..b325fcb4172 100644 --- a/components/infra-proxy-service/storage/postgres/orgs.go +++ b/components/infra-proxy-service/storage/postgres/orgs.go @@ -25,8 +25,13 @@ func (p *postgres) insertOrg(ctx context.Context, projects = []string{} } + // Adding the subjects if missing if gRPC calls from internal service level + subjects := auth_context.FromContext(auth_context.FromIncomingMetadata(ctx)).Subjects + if len(subjects) == 0 { + subjects = []string{"tls:service:compliance-service:internal"} + } _, err := p.authzClient.ValidateProjectAssignment(ctx, &authz.ValidateProjectAssignmentReq{ - Subjects: auth_context.FromContext(auth_context.FromIncomingMetadata(ctx)).Subjects, + Subjects: subjects, OldProjects: []string{}, NewProjects: projects, }) From 209363b1849b6f336f616db5e901253b987acece Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 10 Feb 2021 15:31:19 +0530 Subject: [PATCH 14/16] try with chef-automate dev deployinate Signed-off-by: Vivek Singh --- .expeditor/verify_private.pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.expeditor/verify_private.pipeline.yml b/.expeditor/verify_private.pipeline.yml index 712166956b9..5562cc4147f 100644 --- a/.expeditor/verify_private.pipeline.yml +++ b/.expeditor/verify_private.pipeline.yml @@ -432,7 +432,7 @@ steps: - label: "infra-proxy-service" command: - . scripts/verify_setup.sh - - hab studio run "source scripts/verify_studio_init.sh && start_infra_proxy_service && infra_service_integration" + - hab studio run "source scripts/verify_studio_init.sh && start_deployment_service && chef-automate dev deployinate && infra_service_integration" timeout_in_minutes: 20 retry: automatic: From 8efbd48d7ff0bc19697fe7f406446b2d13edbc3f Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 10 Feb 2021 16:45:26 +0530 Subject: [PATCH 15/16] set CONTAINER_HOSTNAME env var Signed-off-by: Vivek Singh --- .expeditor/verify_private.pipeline.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.expeditor/verify_private.pipeline.yml b/.expeditor/verify_private.pipeline.yml index 5562cc4147f..6f8110f13c5 100644 --- a/.expeditor/verify_private.pipeline.yml +++ b/.expeditor/verify_private.pipeline.yml @@ -444,6 +444,7 @@ steps: environment: - HAB_STUDIO_SUP=false - HAB_NONINTERACTIVE=true + - CONTAINER_HOSTNAME=localhost # # The following tests all use the integration test framework for From 0d3c4c792450cd1a6ca9c4d6c98647712bfecf60 Mon Sep 17 00:00:00 2001 From: Kallol Roy Date: Wed, 10 Feb 2021 19:17:19 +0530 Subject: [PATCH 16/16] Trying to fix integration failure Signed-off-by: Kallol Roy --- .studio/infra-proxy-service | 1 + 1 file changed, 1 insertion(+) diff --git a/.studio/infra-proxy-service b/.studio/infra-proxy-service index b29b7473f0b..ea3d2ca349a 100644 --- a/.studio/infra-proxy-service +++ b/.studio/infra-proxy-service @@ -16,6 +16,7 @@ document "infra_service_integration" <