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

Move alerta api token to header and add option to skip TLS verification. #1053

Merged
merged 1 commit into from
Nov 21, 2016

Conversation

allen13
Copy link

@allen13 allen13 commented Nov 17, 2016

Required for all non-trivial PRs
  • Rebased/mergable
  • Tests pass
  • CHANGELOG.md updated
  • Sign CLA (if not already signed)

Resolves issue #1052

I have tested this change live with the custom kapacitor docker image allen13/kapacitor:latest

Copy link
Contributor

@nathanielc nathanielc left a comment

Choose a reason for hiding this comment

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

Overall this is good. Just a few things have been commented below.

In addition the tests in integrations/streamer_test.go will need to be updated to check for the token in the header instead of the query parameters.

Thanks!

@@ -11,6 +11,8 @@ type Config struct {
Enabled bool `toml:"enabled" override:"enabled"`
// The Alerta URL.
URL string `toml:"url" override:"url"`
// Whether to skip the tls verification of the alerta host
TLSSkipVerify bool `toml:"tls_skip_verify" override:"tls_skip_verify"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you rename this option to InsecureSkipVerify and insecure-skip-verify so that it is consistent with other similar config options?

Copy link
Author

Choose a reason for hiding this comment

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

Yup no problem.

if err != nil {
return err
}

resp, err := http.Post(url, "application/json", post)
client := &http.Client{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create the client in the NewService method so that we don't have to create a new client for each new request?

Since the Alerta config can change dynamically you will also need to create a new client in the Update method if the config changed. And that also means that you will need to synchronize access to the client. For that I would recommend using atomic.Value, see how the config field is access for an example.

Let me know if I can clarify any of that better.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, good catch. I'm pretty sure I understand how the update method works. Let me fix that up.

@allen13
Copy link
Author

allen13 commented Nov 17, 2016

Updated to address above issues.

@nathanielc
Copy link
Contributor

Here is the failing test from server/server_test.go

 TestServer_UpdateConfig (0.25s)
    server_test.go:6230: unexpected defaults for alerta/: alerta: section element 0 are not equal: extra option "insecure-skip-verify" with value <nil>
    server_test.go:6245: unexpected update result 0 for alerta/: alerta: section element 0 are not equal: extra option "insecure-skip-verify" with value <nil>

@allen13
Copy link
Author

allen13 commented Nov 18, 2016

Tests passes this time!

Copy link
Contributor

@nathanielc nathanielc left a comment

Choose a reason for hiding this comment

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

Changes look good. One last thing is the change to reuse the same client creates a race condition. I have commented in-line below on how to fix it. Should be a simple change.

Thanks again!

@@ -17,13 +17,19 @@ import (
type Service struct {
configValue atomic.Value
logger *log.Logger
client *http.Client
Copy link
Contributor

Choose a reason for hiding this comment

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

The client field here should be an atomic.Value type and to be consistent we should call it clientValue.

}

func NewService(c Config, l *log.Logger) *Service {
s := &Service{
logger: l,
}
s.configValue.Store(c)
s.client = &http.Client{
Copy link
Contributor

@nathanielc nathanielc Nov 19, 2016

Choose a reason for hiding this comment

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

Here, use the Store method to store the new client. Much like the configValue line above.

@@ -95,7 +101,13 @@ func (s *Service) Update(newConfig []interface{}) error {
return fmt.Errorf("expected config object to be of type %T, got %T", c, newConfig[0])
} else {
s.configValue.Store(c)
s.client = &http.Client{
Copy link
Contributor

Choose a reason for hiding this comment

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

Same goes here, we should use the Store method.

if err != nil {
return err
}

resp, err := http.Post(url, "application/json", post)
resp, err := s.client.Do(req)
Copy link
Contributor

Choose a reason for hiding this comment

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

Lastly this becomes:

client := s.clientValue.Get().(*http.Client)
resp, err := client.Do(req)

All of these changes avoid a race condition on the client field, synchronizing access to the field.

@allen13
Copy link
Author

allen13 commented Nov 19, 2016

Race condition resolved.

@nathanielc nathanielc merged commit 39b5a8d into influxdata:master Nov 21, 2016
nathanielc added a commit that referenced this pull request Nov 21, 2016
Move alerta api token to header and add option to skip TLS verification.
@allen13
Copy link
Author

allen13 commented Nov 21, 2016

Thanks for the help!

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

Successfully merging this pull request may close these issues.

2 participants