Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .bldr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1122,11 +1122,7 @@ paths = [
"lib/io/*",
"lib/logger/*",
"lib/pgutils/*",
"lib/platform/command/*",
"lib/platform/config/*",
"lib/platform/pg/*",
"lib/platform/sys/*",
"lib/proc/*",
"lib/proxy/*",
"lib/secrets/*",
"lib/simpledatemath/*",
Expand All @@ -1138,7 +1134,6 @@ paths = [
"vendor/github.com/apache/thrift/*",
"vendor/github.com/beorn7/perks/*",
"vendor/github.com/codahale/hdrhistogram/*",
"vendor/github.com/davecgh/go-spew/*",
"vendor/github.com/fsnotify/fsnotify/*",
"vendor/github.com/go-gorp/gorp/*",
"vendor/github.com/gogo/protobuf/*",
Expand All @@ -1161,7 +1156,6 @@ paths = [
"vendor/github.com/pelletier/go-toml/*",
"vendor/github.com/peterbourgon/mergemap/*",
"vendor/github.com/pkg/errors/*",
"vendor/github.com/pmezard/go-difflib/*",
"vendor/github.com/prometheus/client_golang/*",
"vendor/github.com/prometheus/client_model/*",
"vendor/github.com/prometheus/common/*",
Expand All @@ -1173,8 +1167,6 @@ paths = [
"vendor/github.com/spf13/jwalterweatherman/*",
"vendor/github.com/spf13/pflag/*",
"vendor/github.com/spf13/viper/*",
"vendor/github.com/stretchr/objx/*",
"vendor/github.com/stretchr/testify/*",
"vendor/github.com/teambition/rrule-go/*",
"vendor/github.com/uber/jaeger-client-go/*",
"vendor/github.com/uber/jaeger-lib/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"strings"
"time"

_ "github.com/lib/pq"
"github.com/lib/pq"
"github.com/pkg/errors"

"github.com/chef/automate/components/applications-service/pkg/storage"
"github.com/chef/automate/lib/pgutils"
"github.com/chef/automate/lib/platform/pg"
)

// composedService is a more user friendly and clear representation of a service.
Expand Down Expand Up @@ -303,7 +302,7 @@ func (db *Postgres) GetServicesDistinctValues(fieldName, queryFragment string, f
return nil, errors.Errorf("field name %q is not valid for filtering, valid values are %v", fieldName, validFilterFields)
}

columnName := pg.QuoteIdentifier(columnNameForField(fieldName))
columnName := pq.QuoteIdentifier(columnNameForField(fieldName))
queryFirst := fmt.Sprintf("SELECT DISTINCT %[1]s from service_full AS t WHERE t.%[1]s ILIKE $1 ",
columnName,
)
Expand Down
18 changes: 13 additions & 5 deletions components/automate-cli/pkg/diagnostics/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,28 @@ func (c *testContext) PublishViaNATS(messages [][]byte) error {
}

func (c *testContext) adminToken() (string, error) {
if c.Globals.CachedToken != "" {
return c.Globals.CachedToken, nil
}

if c.dsClient == nil {
return "", ErrDeploymentServiceUnavailable
}

if c.Globals.CachedToken == "" {
resp, err := c.dsClient.GenerateAdminToken(context.TODO(), &api.GenerateAdminTokenRequest{
var err error
var resp *api.GenerateAdminTokenResponse

for tries := 0; tries < 3; tries++ {
time.Sleep(time.Duration(tries) * time.Second)
resp, err = c.dsClient.GenerateAdminToken(context.TODO(), &api.GenerateAdminTokenRequest{
Description: "This token was generated by the chef-automate diagnostic tool. " +
"It has admin level access on the entire Automate API.",
})

if err != nil {
return "", err
if err == nil {
c.Globals.CachedToken = resp.ApiToken
break
}
c.Globals.CachedToken = resp.ApiToken
}

return c.Globals.CachedToken, nil
Expand Down
3 changes: 2 additions & 1 deletion components/pg-sidecar-service/integration/pg_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -578,7 +579,7 @@ SELECT EXISTS
)
;`

return c.BoolQuery(fmt.Sprintf(tableOwnedByQuery, pg.QuoteLiteral(role), pg.QuoteLiteral(table)))
return c.BoolQuery(fmt.Sprintf(tableOwnedByQuery, pq.QuoteLiteral(role), pq.QuoteLiteral(table)))
}

func (h *pgHelper) hasSuperuser(role string) (bool, error) {
Expand Down
6 changes: 3 additions & 3 deletions components/pg-sidecar-service/pkg/pgw/alter_role_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/chef/automate/lib/platform/pg"
"github.com/lib/pq"
)

// AlterRoleQuery represents the available options for an ALTER ROLE query
Expand Down Expand Up @@ -94,12 +94,12 @@ func (o *AlterRoleQuery) String() string {
if o.Password.Unencrypted {
options = append(options, "UNENCRYPTED")
}
options = append(options, fmt.Sprintf("PASSWORD %s", pg.QuoteLiteral(o.Password.Value)))
options = append(options, fmt.Sprintf("PASSWORD %s", pq.QuoteLiteral(o.Password.Value)))
}

if len(options) == 0 {
return ""
}

return fmt.Sprintf("ALTER ROLE %s WITH %s", pg.QuoteIdentifier(o.Role), strings.Join(options, " "))
return fmt.Sprintf("ALTER ROLE %s WITH %s", pq.QuoteIdentifier(o.Role), strings.Join(options, " "))
}
10 changes: 5 additions & 5 deletions components/pg-sidecar-service/pkg/pgw/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,15 @@ func (client *Client) startPGRestoreStream(db string, r *io.PipeReader) (func()
}

func (client *Client) grantAllOnSchema(role, schema string) error {
quotedRoleName := pg.QuoteIdentifier(role)
quotedSchemaName := pg.QuoteIdentifier(schema)
quotedRoleName := pq.QuoteIdentifier(role)
quotedSchemaName := pq.QuoteIdentifier(schema)
query := fmt.Sprintf(grantAllOnSchemaQuery, quotedSchemaName, quotedRoleName)
return client.DB.ExecStatement(query)
}

func renderChownAllInSchemaQuery(role, schema string) string {
quotedSchemaName := pg.QuoteLiteral(schema)
quotedOwner := pg.QuoteLiteral(role)
quotedSchemaName := pq.QuoteLiteral(schema)
quotedOwner := pq.QuoteLiteral(role)
return fmt.Sprintf(chownAllInSchemaQuery, quotedSchemaName, quotedOwner)
}
func (client *Client) chownAllInSchema(role, schema string) error {
Expand All @@ -745,7 +745,7 @@ func (client *Client) chownAllInSchema(role, schema string) error {
}

func (client *Client) schemaExists(schema string) (bool, error) {
quotedSchema := pg.QuoteLiteral(schema)
quotedSchema := pq.QuoteLiteral(schema)
query := fmt.Sprintf(schemaExistsQuery, quotedSchema)
return client.DB.BoolQuery(query)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
github.com/leanovate/gopter v0.2.4
github.com/lib/pq v0.0.0-20171126050459-83612a56d3dd
github.com/lib/pq v1.2.0
github.com/lyft/protoc-gen-star v0.4.11
github.com/magiconair/properties v1.7.4 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhR
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/leanovate/gopter v0.2.4 h1:U4YLBggDFhJdqQsG4Na2zX7joVTky9vHaj/AGEwSuXU=
github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/lib/pq v0.0.0-20171126050459-83612a56d3dd h1:2RDaVc4/izhWyAvYxNm8c9saSyCDIxefNwOcqaH7pcU=
github.com/lib/pq v0.0.0-20171126050459-83612a56d3dd/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lyft/protoc-gen-star v0.4.11 h1:zW6fJQBtCtVeSiO/Kbpzv32GO0J/Z8egSLeohES202w=
github.com/lyft/protoc-gen-star v0.4.11/go.mod h1:mE8fbna26u7aEA2QCVvvfBU/ZrPgocG1206xAFPcs94=
github.com/magiconair/properties v1.7.4 h1:UVo0TkHGd4lQSN1dVDzs9URCIgReuSIcCXpAVB9nZ80=
Expand Down
8 changes: 7 additions & 1 deletion integration/tests/cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ do_test_deploy() {
export ELASTICSEARCH_URL="http://$frontend1_ip:10144"
test_notifications_endpoint="http://$test_container_ip:15555"

run_inspec_tests "${A2_ROOT_DIR}" "a2-iam-v2-integration"
# Inspec tests are less tolerant of transient 500s; we expect a few of
# those as bad postgres connections get purged from the various services.
# Restart everything before running the tests
#docker exec -t "$_frontend1_container_name" "$cli_bin" restart-services
#docker exec -t "$_frontend2_container_name" "$cli_bin" restart-services

run_inspec_tests "${A2_ROOT_DIR}" "a2-iam-v2-integration"
local admin_token
admin_token=$(docker exec -t "$_frontend1_container_name" \
"$cli_bin" iam token create --admin "diagnostics-test-$RANDOM")
Expand All @@ -106,6 +111,7 @@ do_test_deploy() {
"$cli_bin" diagnostics run --admin-token "$admin_token" "~iam-v1" "~applications"

"$cli_bin" diagnostics run --admin-token "$admin_token" "~iam-v1" "~purge" "~cli" "~grpc" "~deployment" "~applications"

}

do_dump_logs() {
Expand Down
71 changes: 16 additions & 55 deletions lib/platform/pg/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package pg

import (
"fmt"
"strings"

"github.com/lib/pq"
)

// Queries that don't support positional arguments and thus have
Expand Down Expand Up @@ -39,91 +40,51 @@ const (
)

func CreateDatabaseQuery(dbname string) string {
quotedDBname := QuoteIdentifier(dbname)
quotedDBname := pq.QuoteIdentifier(dbname)
return fmt.Sprintf(createDBQuery, quotedDBname)
}

func CreateDatabaseWithOwnerQuery(dbname string, owner string) string {
quotedDBname := QuoteIdentifier(dbname)
quotedOwner := QuoteIdentifier(owner)
quotedDBname := pq.QuoteIdentifier(dbname)
quotedOwner := pq.QuoteIdentifier(owner)
return fmt.Sprintf(createDBWithOwnerQuery, quotedDBname, quotedOwner)
}

func AlterDatabaseOwner(dbname string, owner string) string {
quotedDBname := QuoteIdentifier(dbname)
quotedOwner := QuoteIdentifier(owner)
quotedDBname := pq.QuoteIdentifier(dbname)
quotedOwner := pq.QuoteIdentifier(owner)
return fmt.Sprintf(alterDBOwner, quotedDBname, quotedOwner)
}

func DropDatabaseQuery(dbname string) string {
quotedDBName := QuoteIdentifier(dbname)
quotedDBName := pq.QuoteIdentifier(dbname)
return fmt.Sprintf(dropDBQuery, quotedDBName)
}

func CreateRoleQuery(name string) string {
quotedRoleName := QuoteLiteral(name)
quotedRoleIdent := QuoteIdentifier(name)
quotedRoleName := pq.QuoteLiteral(name)
quotedRoleIdent := pq.QuoteIdentifier(name)
return fmt.Sprintf(createRoleQuery, quotedRoleName, quotedRoleIdent)
}

func RemoveRolePasswordQuery(name string) string {
quotedRoleIdent := QuoteIdentifier(name)
quotedRoleIdent := pq.QuoteIdentifier(name)
return fmt.Sprintf(removePWQuery, quotedRoleIdent)
}

func RenameDatabaseQuery(old string, new string) string {
quotedOld := QuoteIdentifier(old)
quotedNew := QuoteIdentifier(new)
quotedOld := pq.QuoteIdentifier(old)
quotedNew := pq.QuoteIdentifier(new)
return fmt.Sprintf(renameDBQuery, quotedOld, quotedNew)
}

func GrantAllQuery(dbName string, roleName string) string {
quotedDBName := QuoteIdentifier(dbName)
quotedRoleName := QuoteIdentifier(roleName)
quotedDBName := pq.QuoteIdentifier(dbName)
quotedRoleName := pq.QuoteIdentifier(roleName)
return fmt.Sprintf(grantAllQuery, quotedDBName, quotedRoleName)
}

func CreateExtensionQuery(extName string) string {
quotedExtName := QuoteIdentifier(extName)
quotedExtName := pq.QuoteIdentifier(extName)
return fmt.Sprintf(createExtensionQuery, quotedExtName)
}

// Taken from: https://github.com/lib/pq/pull/718
// which seems close enough to
//
// https://github.com/postgres/postgres/blob/322548a8abe225f2cfd6a48e07b99e2711d28ef7/src/backend/utils/adt/quote.c#L46-L70
//
// to be trustworthy.
//
// QuoteLiteral quotes a string literal to be used as part of an SQL statement.
// It's useful with SQL statements that don't support parametrization.
// For example:
//
// quoted := pq.QuoteLiteral("secret")
// err := db.Exec(fmt.Sprintf("CREATE USER foo PASSWORD %s", quoted))
//
// Any single quotes and backslashes in value will be escaped. If value contains
// at least one backslash, "E" prefix will be prepended.
func QuoteLiteral(value string) string {
prefix := ""
if strings.Contains(value, `\`) {
prefix = "E"
}
value = strings.Replace(value, "'", "''", -1)
value = strings.Replace(value, `\`, `\\`, -1)
return prefix + "'" + value + "'"
}

// Quote a postgresql identifier. Identifiers are quoted with double
// quotes in postgresql.
//
// The postgresql function that does this
// https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/ruleutils.c#L10550
//
// keeps identifiers unquoted if they only contain safe
// characters. Here, we just always quote since we use this in places
// where quoted identifiers are always allowed.
func QuoteIdentifier(value string) string {
value = strings.Replace(value, `"`, `""`, -1)
return fmt.Sprintf(`"%s"`, value)
}
25 changes: 0 additions & 25 deletions vendor/github.com/lib/pq/.travis.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/lib/pq/buf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading