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

loki-canary: Add support for client-side TLS certs for Loki connection #6310

Merged
merged 4 commits into from
Jun 9, 2022

Conversation

chodges15
Copy link
Contributor

What this PR does / why we need it:
Support for loki-canary in environments using mTLS, in anticipation of deny-by-default Loki server authentication coming for #6283.

Which issue(s) this PR fixes:
Fixes #6295

Checklist

  • Is this an important fix or new feature? Add an entry in the CHANGELOG.md.

@chodges15 chodges15 requested a review from a team as a code owner June 3, 2022 21:44
@CLAassistant
Copy link

CLAassistant commented Jun 3, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

LGTM! (there's a lint issue tho)

pkg/canary/reader/reader.go Show resolved Hide resolved
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jun 6, 2022
Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

one more, sorry

CHANGELOG.md Outdated Show resolved Hide resolved
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

1 similar comment
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Collaborator

@trevorwhitney trevorwhitney left a comment

Choose a reason for hiding this comment

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

LGTM! definitely something we need!

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0.1%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
-               loki	-0.6%

@chodges15
Copy link
Contributor Author

@DylanGuedes and @trevorwhitney thanks for the quick review, I got the CLA signed (and updated all commits with same username) and the PR should now be ready to merge.

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @chodges15! Just one suggestion.

func (r *Reader) webSocketDialer() *websocket.Dialer {
dialer := websocket.DefaultDialer
if r.clientTLSConfig != nil {
dialer.TLSClientConfig = r.clientTLSConfig
Copy link
Member

Choose a reason for hiding this comment

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

We probably shouldn't be mutating websocket.DefaultDialer in this library package.

Instead, let's create a new one like:

var dialer = &Dialer{
	Proxy:            http.ProxyFromEnvironment,
	HandshakeTimeout: 45 * time.Second,
	TLSClientConfig = r.clientTLSConfig,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding is that dialer is a copy of websocket.DefaultDialer and there is no mutation happening outside this package. However, I like your way better as it keeps me from having to do local mutation.

Comment on lines +92 to +111
var tlsConfig *tls.Config
tc := config.TLSConfig{}
if *certFile != "" || *keyFile != "" || *caFile != "" {
if !*useTLS {
_, _ = fmt.Fprintf(os.Stderr, "Must set --tls when specifying client certs\n")
os.Exit(1)
}
tc.CAFile = *caFile
tc.CertFile = *certFile
tc.KeyFile = *keyFile
tc.InsecureSkipVerify = false

var err error
tlsConfig, err = config.NewTLSConfig(&tc)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "TLS configuration error: %s\n", err.Error())
os.Exit(1)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm missing anything. We could simplify this initialization code bit.

the gist is

  1. to first use if useTLS to do all this and skip everything completely (even before checking for *certFile, *keyFile, *caFile.
  2. And trusting config.NewTLSConfig for checking if all the files are invalid (even if its empty)

Something simple like

	if useTLS {
		tls, err := config.NewTLSConfig(
			&config.TLSConfig{
				CAFile, *caFile,
				CertFile, *certFile,
				KeyFile, *keyFile,
				InsecureSkipVerify: false,
			},
		)
		if err != nil {
			// print error
			os.Exit(1)
		}
	}
}

No need to check for any empty string on those cert files. NewTLSConfig() should return error in that case correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main reason for doing this was to ensure valid configuration. Setting the client certs requires -tls to be set otherwise it is a noop. I actually made this mistake in my own testing and was glad to have the error message. On the other hand, when setting -tls it does not require setting client certs.

Copy link
Contributor

Choose a reason for hiding this comment

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

On the other hand, when setting -tls it does not require setting client certs.

Ah make sense. In that case we just use default client. thanks for the clarification.

resp, err := http.DefaultClient.Do(req)
httpClient, err := r.httpClient()
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

I would highly recommend wrapping the error here. (and also on the QueryCountOverTime) with some context.

Rationale is I see we use it on both Query and QueryCountOverTime and it can confuse the consumer of the error, where exactly it is coming from.

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

-           ingester	-0.1%
-        distributor	-0.3%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@kavirajk
Copy link
Contributor

kavirajk commented Jun 9, 2022

@chodges15 also addressed your suggestion @owen-d. I think it's good to go. Merging it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[loki-canary] Add ability to load client certs for reader TLS connection to Loki
7 participants