Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

implement postgres support for TLS #3488

Merged
merged 39 commits into from
Oct 14, 2020
Merged

implement postgres support for TLS #3488

merged 39 commits into from
Oct 14, 2020

Conversation

nadilas
Copy link
Contributor

@nadilas nadilas commented Aug 29, 2020

TLS support implemented for postgres plugin

The changes were tested by deploying cadence and cadence_visibility to a clean postgres database using TLS connection via the cadence-sql-tool

Potential risks
Backwards compatibility for password authentication to be tested by test-suite on a dev database instance

@CLAassistant
Copy link

CLAassistant commented Aug 29, 2020

CLA assistant check
All committers have signed the CLA.

if attrs != "" {
dsn += "?" + attrs
}
return dsn
}

// TODO: implement postgres specific support for TLS
Copy link
Contributor

@longquanzheng longquanzheng Sep 3, 2020

Choose a reason for hiding this comment

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

Should we remove the TODO? Or remove the method if it's not being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could. The only reason I kept it in, because I thought it to be an interface implementation.
Although I must say, I did a pass at temporal as well and the source files were slightly different, I might do another commit before going forward to synchronize the two directly, if that’s okay for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not quite follow exactly. Another commit for this? I thought this commit is working already. Or do you mean for other purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about the confusion, it does work, however I want to synchronize with https://github.com/temporalio/temporal/blob/master/common/persistence/sql/sqlplugin/postgres/plugin.go so I will do an additional commit tomorrow, before we go ahead with this one. I hope that makes sense.

@longquanzheng
Copy link
Contributor

Thank you for making this @nadilas for us!

// Maps struct names in CamelCase to snake without need for db struct tags.
db.MapperFunc(strcase.ToSnake)
return db, nil
dsn := fmt.Sprintf(dsnFmt, "postgresql", cfg.User, host, port, dbName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Tests are failling. Seems like this is not compatible with pq server that without TSL enabled:


FAIL: TestSQLHistoryV2PersistenceSuite (0.00s) | 1s
-- | --
  | panic: pq: SSL is not enabled on the server [recovered]
  | panic: pq: SSL is not enabled on the server
  |  
  | goroutine 10 [running]:
  | testing.tRunner.func1(0xc000392300)
  | /usr/local/go/src/testing/testing.go:874 +0x69f
  | panic(0x2030fe0, 0xc0000b8880)
  | /usr/local/go/src/runtime/panic.go:679 +0x1b2
  | github.com/uber/cadence/common/persistence/sql.(*TestCluster).CreateDatabase(0xc0005a4840)
  | /cadence/common/persistence/sql/sqlPersistenceTest.go:113 +0x2df
  | github.com/uber/cadence/common/persistence/sql.(*TestCluster).SetupTestDatabase(0xc0005a4840)
  | /cadence/common/persistence/sql/sqlPersistenceTest.go:74 +0x51
  | github.com/uber/cadence/common/persistence/persistence-tests.(*TestBase).Setup(0xc0003c2658)
  | /cadence/common/persistence/persistence-tests/persistenceTestBase.go:178 +0xdf
  | github.com/uber/cadence/common/persistence/sql/sqlplugin/postgres.TestSQLHistoryV2PersistenceSuite(0xc000392300)
  | /cadence/common/persistence/sql/sqlplugin/postgres/postgres_server_test.go:68 +0xed
  | testing.tRunner(0xc000392300, 0x22e8f98)
  | /usr/local/go/src/testing/testing.go:909 +0x19a
  | created by testing.(*T).Run
  | /usr/local/go/src/testing/testing.go:960 +0x652
  | FAIL	github.com/uber/cadence/common/persistence/sql/sqlplugin/postgres	0.041s
  | FAIL
  | make: *** [Makefile:196: cover_profile] Error 1
  | ⚠️ Failed to run command, exited with 2


Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just made the necessary adjustments, can you please test again?

Copy link
Contributor

Choose a reason for hiding this comment

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

Still not passing:


panic: pq: password authentication failed for user "postgres" [recovered]
--
  | panic: pq: password authentication failed for user "postgres"


You can see it if you click the details in
buildkite/cadence-server/pr/golang-unit-test — Failed (exit status 2)

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you able to run the test locally? That will be easier to debug and fix the test.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah e2e tests

Sure since we have it in this repo. The reason we didn't do that is that we ran into timing issues with buildkite. But it should be fine since we are running tests in parallel now.

@meiliang86
Copy link
Contributor

Unrelated to this, we might want to add the pg tests to buildkite, similar to what we do for MySQL. @longquanzheng what do you think?

@longquanzheng
Copy link
Contributor

Unrelated to this, we might want to add the pg tests to buildkite, similar to what we do for MySQL. @longquanzheng what do you think?

https://github.com/uber/cadence/blob/master/docker/buildkite/docker-compose.yml
I thought we already have pg for running "unit" test, but not e2e integ test. Do you mean that?
We could let it run too.

@meiliang86
Copy link
Contributor

yeah e2e tests

@meiliang86
Copy link
Contributor

@nadilas unit tests are still failing. Can you check?

FAIL: TestSQLHistoryV2PersistenceSuite (0.00s) | 1s
-- | --
  | panic: pq: password authentication failed for user "postgres" [recovered]
  | panic: pq: password authentication failed for user "postgres"
  |  
  | goroutine 13 [running]:
  | testing.tRunner.func1(0xc000300200)
  | /usr/local/go/src/testing/testing.go:874 +0x69f
  | panic(0x207e420, 0xc0003b4000)
  | /usr/local/go/src/runtime/panic.go:679 +0x1b2
  | github.com/uber/cadence/common/persistence/sql.(*TestCluster).CreateDatabase(0xc0005ac6e0)
  | /cadence/common/persistence/sql/sqlPersistenceTest.go:113 +0x2df
  | github.com/uber/cadence/common/persistence/sql.(*TestCluster).SetupTestDatabase(0xc0005ac6e0)
  | /cadence/common/persistence/sql/sqlPersistenceTest.go:74 +0x51
  | github.com/uber/cadence/common/persistence/persistence-tests.(*TestBase).Setup(0xc00003e798)
  | /cadence/common/persistence/persistence-tests/persistenceTestBase.go:178 +0xdf
  | github.com/uber/cadence/common/persistence/sql/sqlplugin/postgres.TestSQLHistoryV2PersistenceSuite(0xc000300200)
  | /cadence/common/persistence/sql/sqlplugin/postgres/postgres_server_test.go:68 +0xed
  | testing.tRunner(0xc000300200, 0x22e90f8)
  | /usr/local/go/src/testing/testing.go:909 +0x19a
  | created by testing.(*T).Run
  | /usr/local/go/src/testing/testing.go:960 +0x652
  | FAIL	github.com/uber/cadence/common/persistence/sql/sqlplugin/postgres	0.043s


dataSourceNamePostgres = "user=%v host=%v port=%v dbname=%v sslmode=disable %v "
dataSourceNamePostgresPassword = "password=%v"
PluginName = "postgres"
dsnFmt = "postgres://%s@%s:%s/%s"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the test failure because this is not compatible with basic authentication?

@longquanzheng
Copy link
Contributor

Thank you very much for making this PR.

@nadilas Hey I added more document to https://github.com/uber/cadence/blob/master/CONTRIBUTING.md
Hope it will help contributors from external like you!

Or I can take this over, if it's too complex for you :P

@nadilas
Copy link
Contributor Author

nadilas commented Sep 29, 2020

I’m sorry @longquanzheng Life happened, I got sidetracked by my day job 😅
I’ll clean this up this weekend.

But if you have it urgent feel free to step in.

@longquanzheng
Copy link
Contributor

I’m sorry @longquanzheng Life happened, I got sidetracked by my day job 😅
I’ll clean this up this weekend.

But if you have it urgent feel free to step in.

Oh thanks for the reply. It's not urgent at all. Feel free to slack me when you got stuck. It's not really easy thing to start development for Cadence project. We really love to have your contribution.

@longquanzheng
Copy link
Contributor

@meiliang86 I just rebased the PR, can you help kick off the test?

@longquanzheng
Copy link
Contributor

@nadilas looks like my rebase at Github UI doens't work, we need to do rebase manually so re-sync the go.mod. I don't have permission to push into your branch, could you rebase it?

@nadilas
Copy link
Contributor Author

nadilas commented Oct 13, 2020

@longquanzheng I updated the branch, let's try this again :)

@longquanzheng
Copy link
Contributor

longquanzheng commented Oct 13, 2020

@longquanzheng I updated the branch, let's try this again :)

It failed at :

make: *** [Makefile:171: fmt] Error 1

Can you run

make fmt

to re-format the code?
(I just made a PR to update our contribute guide to include that instruction: https://github.com/uber/cadence/pull/3642/files)

…/plugin.go

removes duplicate getTestClusterSetup() from postgres_server_test.go
ran make fmt
@nadilas
Copy link
Contributor Author

nadilas commented Oct 14, 2020

@longquanzheng ready to rumble.

@longquanzheng longquanzheng merged commit 0ea68b8 into cadence-workflow:master Oct 14, 2020
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 65.141% when pulling 0439167 on nadilas:patch-1 into ba0eacb on uber:master.

@meiliang86
Copy link
Contributor

Thanks @nadilas and @longquanzheng !

sslParams = url.Values{}
if cfg.TLS != nil && cfg.TLS.Enabled {
sslParams.Set("ssl", "true")
sslParams.Set("sslmode", "require")
Copy link

@JosefWN JosefWN Nov 20, 2020

Choose a reason for hiding this comment

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

I know I'm late to the party, but is it wise to hard code this? How would I connect using the more secure modes verify-ca or verify-full for example?

Could as well let the user choose?

sslParams.Set("sslmode", cfg.TLS.SSLMode)

Just looking at the config it's hard to know what cfg.TLS.Enabled means; is it allow, prefer, require, verify-ca or verify-full? All of those modes are enabling TLS support to various degrees.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @JosefWN you are not late! Apparently I don't have enough expertise/experience about security so I didn't think too much about that.
Is value of sslmode the only thing needed to change to support all other modes like you mention? If so then I could make a PR out for this.

Or it would be nice if you can make a PR for this. Because I am not sure how to test it.

Copy link

Choose a reason for hiding this comment

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

I think making the sslmode configurable is sufficient (which would also make cfg.TLS.Enabled redundant).

Perhaps something like this:

	sslParams.Set("sslmode", cfg.SSLMode)
	if cfg.SSLMode != "disable" && cfg.TLS != nil {
		sslParams.Set("sslrootcert", cfg.TLS.CaFile)
		sslParams.Set("sslkey", cfg.TLS.KeyFile)
		sslParams.Set("sslcert", cfg.TLS.CertFile)
	}

The default is prefer, so it should be backwards compatible even if SSLMode is not set (but would need to test that it actually works).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks a lot! PR out here: https://github.com/uber/cadence/pull/3787/files
But I think we have to make default as require now...since people started to use it. I believe it's okay, lmk if I am wrong.

github-actions bot pushed a commit to vytautas-karpavicius/cadence that referenced this pull request Feb 4, 2021
yux0 pushed a commit to yux0/cadence that referenced this pull request May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.