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

database/postgres: add inline certificate authentication fields #28024

Merged
merged 7 commits into from
Aug 9, 2024

Conversation

fairclothjm
Copy link
Contributor

@fairclothjm fairclothjm commented Aug 7, 2024

This PR adds support for inline TLS configuration.

We add new fields to the plugin's config endpoint: tls_certificate, tls_private_key and tls_ca. These fields will take certificate data as a string. This allows Vault Operators and/or development teams to configure TLS when they do not have access to the Vault Server.

Here is an example of configuring TLS inline via the new fields on the plugin’s config endpoint:

CONN_URL="user='{{username}}' database='postgres' \
  host='localhost' port='5432' sslmode='verify-full'"

vault write database/config/postgres-db \
  plugin_name=postgresql-database-plugin \
  allowed_roles="*" \
  connection_url="$CONN_URL" \
  username=client \
  tls_certificate="@/path/to/client.crt" \
  tls_private_key="@/path/to/client.key" \
  tls_ca="@/path/to/ca.crt"

The @ in the CLI argument indicates a path to a file on disk to be read.

@fairclothjm fairclothjm added this to the 1.18.0-rc milestone Aug 7, 2024
@fairclothjm fairclothjm requested a review from a team as a code owner August 7, 2024 22:15
@github-actions github-actions bot added the hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed label Aug 7, 2024
@fairclothjm fairclothjm force-pushed the postgres-ssl-plugin-fields branch from da01aee to 83748a8 Compare August 7, 2024 22:16
Copy link

github-actions bot commented Aug 7, 2024

CI Results:
All Go tests succeeded! ✅

@@ -62,15 +58,3 @@ func GetCloudSQLAuthOptions(credentials string, usePrivateIP bool) ([]cloudsqlco

return opts, nil
}

func ValidateAuthType(authType string) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to sql.go

Copy link

github-actions bot commented Aug 7, 2024

Build Results:
All builds succeeded! ✅

@fairclothjm fairclothjm force-pushed the postgres-ssl-plugin-fields branch from 83748a8 to 889ed2f Compare August 7, 2024 22:17
// Deprecated: OpenPostgres will be removed in a future version of the Vault SDK.
func OpenPostgres(driverName, connString string) (*sql.DB, error) {
// Deprecated: openPostgres will be removed in a future version of the Vault SDK.
func openPostgres(driverName, connString string) (*sql.DB, error) {
Copy link
Contributor Author

@fairclothjm fairclothjm Aug 7, 2024

Choose a reason for hiding this comment

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

This was only exported for tests but I decided we should not export this so the test usage was removed. This code path is tested however because it is called indirectly by our tests.

Copy link
Contributor

@vinay-gopalan vinay-gopalan left a comment

Choose a reason for hiding this comment

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

LGTM! Had 1 question, but non-blocking

}
tlsConfig.Certificates = []tls.Certificate{cert}
p.TLSConfig = tlsConfig
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are sslcert and sslkey always required to have a TLS config? Should it be possible for the user to only set the sslrootcert? If not, wondering if it is worth returning an error to the user saying that the TLS Cert and Private Key need to be added

This is for the case if the user sets the sslrootcert but does not set either the sslcert or sslkey, then p.TLSConfig will be nil, and we would not use any info decoded in L112-L119. Since it would be under the hood I was wondering if it would be worth surfacing that we will ignore the root cert because of missing info in the config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question! I considered this briefly as I was writing this code but never came back to it.

Setting only sslrootcert is a legit use case. This is useful when you want the client to verify the server certificate. See https://www.postgresql.org/docs/16/libpq-ssl.html#LIBQ-SSL-CERTIFICATES

I will update this flow to allow for this use case. 👍

@fairclothjm fairclothjm merged commit 3fcb1a6 into main Aug 9, 2024
83 checks passed
@fairclothjm fairclothjm deleted the postgres-ssl-plugin-fields branch August 9, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants