From cd177768ed06925c57ad39bd2aaa87e3db401acf Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Thu, 2 Jul 2020 13:45:26 +0200 Subject: [PATCH 1/5] Fix folder naming for changelog --- changelog/{unreleased => 0.1.0_2020-06-21}/initial-release.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{unreleased => 0.1.0_2020-06-21}/initial-release.md (100%) diff --git a/changelog/unreleased/initial-release.md b/changelog/0.1.0_2020-06-21/initial-release.md similarity index 100% rename from changelog/unreleased/initial-release.md rename to changelog/0.1.0_2020-06-21/initial-release.md From 6e4b653ec2cdfa5ca78dcfadd739dcdccf7470e4 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Thu, 2 Jul 2020 13:46:26 +0200 Subject: [PATCH 2/5] Fix authentication timeout/invalidation --- CHANGELOG.md | 24 ++++++++++- changelog/0.1.1_2020-07-02/auth-timeout.md | 8 ++++ pkg/action/discoverer.go | 37 +++++++++------- pkg/action/server.go | 27 +++--------- pkg/client/client.go | 49 ++++++++++++++++++++++ 5 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 changelog/0.1.1_2020-07-02/auth-timeout.md create mode 100644 pkg/client/client.go diff --git a/CHANGELOG.md b/CHANGELOG.md index ca6a5e0..cc6e68c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,26 @@ -# Changelog for unreleased +# Changelog for 0.1.1 -The following sections list the changes for unreleased. +The following sections list the changes for 0.1.1. + +## Summary + + * Fix #2: Fix authentication timeout/invalidation + +## Details + + * Bugfix #2: Fix authentication timeout/invalidation + + When running the service it happened that the authentication had been invalidated or simply + timed out, this should be fixed by simply authenticating the defined user before looping + through all the results. At the end also the disconnect function from the used library gets + executed. + + https://github.com/promhippie/prometheus-vcd-sd/issues/2 + + +# Changelog for 0.1.0 + +The following sections list the changes for 0.1.0. ## Summary diff --git a/changelog/0.1.1_2020-07-02/auth-timeout.md b/changelog/0.1.1_2020-07-02/auth-timeout.md new file mode 100644 index 0000000..03a29fd --- /dev/null +++ b/changelog/0.1.1_2020-07-02/auth-timeout.md @@ -0,0 +1,8 @@ +Bugfix: Fix authentication timeout/invalidation + +When running the service it happened that the authentication had been +invalidated or simply timed out, this should be fixed by simply authenticating +the defined user before looping through all the results. At the end also the +disconnect function from the used library gets executed. + +https://github.com/promhippie/prometheus-vcd-sd/issues/2 diff --git a/pkg/action/discoverer.go b/pkg/action/discoverer.go index 72a70a0..c58713b 100644 --- a/pkg/action/discoverer.go +++ b/pkg/action/discoverer.go @@ -12,7 +12,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/vmware/go-vcloud-director/v2/govcd" + "github.com/promhippie/prometheus-vcd-sd/pkg/client" ) const ( @@ -33,21 +33,11 @@ const ( var ( // ErrClientEndpoint defines an error if the client auth fails. ErrClientEndpoint = errors.New("failed to parse api url") - - // ErrClientAuth defines an error if the client auth fails. - ErrClientAuth = errors.New("failed to authenticate client") ) -// Config wraps the vCloud Director client including org and vdc names. -type Config struct { - client *govcd.VCDClient - org string - vdc string -} - // Discoverer implements the Prometheus discoverer interface. type Discoverer struct { - configs map[string]*Config + configs map[string]*client.Client logger log.Logger refresh int separator string @@ -79,8 +69,21 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro targets := make([]*targetgroup.Group, 0) for project, config := range d.configs { + if err := config.Authenticate(); err != nil { + level.Warn(d.logger).Log( + "msg", "Failed to authenticate", + "project", project, + "err", err, + ) + + requestFailures.WithLabelValues(project, "auth").Inc() + continue + } + + defer config.Disconnect() + nowOrg := time.Now() - org, err := config.client.GetOrgByNameOrId(config.org) + org, err := config.Upstream.GetOrgByNameOrId(config.Organization) requestDuration.WithLabelValues(project, "org").Observe(time.Since(nowOrg).Seconds()) if err != nil { @@ -95,7 +98,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro } nowVdc := time.Now() - vdc, err := org.GetVDCByNameOrId(config.vdc, false) + vdc, err := org.GetVDCByNameOrId(config.Datacenter, false) requestDuration.WithLabelValues(project, "vdc").Observe(time.Since(nowVdc).Seconds()) if err != nil { @@ -194,8 +197,8 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro Labels: model.LabelSet{ model.AddressLabel: model.LabelValue(vm.VM.NetworkConnectionSection.NetworkConnection[0].IPAddress), model.LabelName(projectLabel): model.LabelValue(project), - model.LabelName(orgLabel): model.LabelValue(config.org), - model.LabelName(vdcLabel): model.LabelValue(config.vdc), + model.LabelName(orgLabel): model.LabelValue(config.Organization), + model.LabelName(vdcLabel): model.LabelValue(config.Datacenter), model.LabelName(nameLabel): model.LabelValue(vm.VM.Name), model.LabelName(statusLabel): model.LabelValue(strconv.Itoa(vm.VM.Status)), }, @@ -234,6 +237,8 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro targets = append(targets, target) } } + + config.Disconnect() } for k := range d.lasts { diff --git a/pkg/action/server.go b/pkg/action/server.go index feb170f..99e5bb0 100644 --- a/pkg/action/server.go +++ b/pkg/action/server.go @@ -15,10 +15,10 @@ import ( "github.com/oklog/run" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/promhippie/prometheus-vcd-sd/pkg/adapter" + "github.com/promhippie/prometheus-vcd-sd/pkg/client" "github.com/promhippie/prometheus-vcd-sd/pkg/config" "github.com/promhippie/prometheus-vcd-sd/pkg/middleware" "github.com/promhippie/prometheus-vcd-sd/pkg/version" - "github.com/vmware/go-vcloud-director/v2/govcd" ) // Server handles the server sub-command. @@ -35,7 +35,7 @@ func Server(cfg *config.Config, logger log.Logger) error { { ctx := context.Background() - configs := make(map[string]*Config, len(cfg.Target.Credentials)) + configs := make(map[string]*client.Client, len(cfg.Target.Credentials)) for _, credential := range cfg.Target.Credentials { parsed, err := url.ParseRequestURI(credential.URL) @@ -49,29 +49,14 @@ func Server(cfg *config.Config, logger log.Logger) error { return ErrClientEndpoint } - client := govcd.NewVCDClient( - *parsed, + configs[credential.Project] = client.New( + parsed, credential.Insecure, - ) - - if err := client.Authenticate( credential.Username, credential.Password, credential.Org, - ); err != nil { - level.Error(logger).Log( - "msg", ErrClientAuth, - "project", credential.Project, - ) - - return ErrClientAuth - } - - configs[credential.Project] = &Config{ - client: client, - org: credential.Org, - vdc: credential.Vdc, - } + credential.Vdc, + ) } disc := Discoverer{ diff --git a/pkg/client/client.go b/pkg/client/client.go new file mode 100644 index 0000000..76e7783 --- /dev/null +++ b/pkg/client/client.go @@ -0,0 +1,49 @@ +package client + +import ( + "net/url" + + "github.com/vmware/go-vcloud-director/v2/govcd" +) + +// New initializes a new client. +func New(endpoint *url.URL, insecure bool, username, password, org, vdc string) *Client { + return &Client{ + Upstream: govcd.NewVCDClient( + *endpoint, + insecure, + ), + Endpoint: endpoint, + Insecure: insecure, + Username: username, + Password: password, + Organization: org, + Datacenter: vdc, + } +} + +// Client abstracts some cloud provider client handling. +type Client struct { + Upstream *govcd.VCDClient + + Endpoint *url.URL + Insecure bool + Username string + Password string + Organization string + Datacenter string +} + +// Authenticate wraps the auth for the cloud provider. +func (c *Client) Authenticate() error { + return c.Upstream.Authenticate( + c.Username, + c.Password, + c.Organization, + ) +} + +// Disconnect wraps the logout for the cloud provider. +func (c *Client) Disconnect() error { + return c.Upstream.Disconnect() +} From 1efb38556826030246bc0f7b531e37923717ed7d Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Thu, 2 Jul 2020 13:56:41 +0200 Subject: [PATCH 3/5] Add codacy config file --- .codacy.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .codacy.yml diff --git a/.codacy.yml b/.codacy.yml new file mode 100644 index 0000000..1fba27f --- /dev/null +++ b/.codacy.yml @@ -0,0 +1,7 @@ +--- +exclude_paths: + - .github/*.md + - CHANGELOG.md + - changelog/**/*.md + +... From aa98fbb5a07b9312693d580fba9f2314257936b8 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Thu, 2 Jul 2020 14:07:58 +0200 Subject: [PATCH 4/5] Use a standard formatted CoC, fix readme linting --- CODE_OF_CONDUCT.md | 151 ++++++++++++++++++++++++++++++--------------- README.md | 4 +- 2 files changed, 104 insertions(+), 51 deletions(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 95fb1fe..dd096b8 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,76 +1,129 @@ + # Contributor Covenant Code of Conduct ## Our Pledge -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, sex characteristics, gender identity and expression, -level of experience, education, socio-economic status, nationality, personal -appearance, race, religion, or sexual identity and orientation. +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. ## Our Standards -Examples of behavior that contributes to creating a positive environment -include: +Examples of behavior that contributes to a positive environment for our +community include: -- Using welcoming and inclusive language -- Being respectful of differing viewpoints and experiences -- Gracefully accepting constructive criticism -- Focusing on what is best for the community -- Showing empathy towards other community members +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community -Examples of unacceptable behavior by participants include: +Examples of unacceptable behavior include: -- The use of sexualized language or imagery and unwelcome sexual attention or - advances -- Trolling, insulting/derogatory comments, and personal or political attacks -- Public or private harassment -- Publishing others' private information, such as a physical or electronic - address, without explicit permission -- Other conduct which could reasonably be considered inappropriate in a - professional setting +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting -## Our Responsibilities +## Enforcement Responsibilities -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. ## Scope -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at [promhippie@webhippie.de.](mailto:promhippie@webhippie.de.) All -complaints will be reviewed and investigated and will result in a response that -is deemed necessary and appropriate to the circumstances. The project team is -obligated to maintain confidentiality with regard to the reporter of an incident. -Further details of specific enforcement policies may be posted separately. +reported to the community leaders responsible for enforcement at +promhippie@webhippie.de. All complaints will be reviewed and investigated +promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. ## Attribution -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, -available at +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). [homepage]: https://www.contributor-covenant.org -For answers to common questions about this code of conduct, see - +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/README.md b/README.md index 8f7fdc2..db14d34 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The following list of meta labels can be used to relabel your scrape results ent Here you get a snippet for the Prometheus `scrape_config` that configures Prometheus to scrape `node_exporter` assuming that it is deployed on all your servers. -``` +```yml - job_name: node file_sd_configs: - files: [ "/etc/prometheus/vcd.json" ] @@ -54,7 +54,7 @@ Here you get a snippet for the Prometheus `scrape_config` that configures Promet Make sure you have a working Go environment, for further reference or a guide take a look at the [install instructions](http://golang.org/doc/install.html). This project requires Go >= v1.11. -```bash +```console git clone https://github.com/promhippie/prometheus-vcd-sd.git cd prometheus-vcd-sd From ee058d7dba44ec52ee4d8d390ae8c111b133411b Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Thu, 2 Jul 2020 14:13:19 +0200 Subject: [PATCH 5/5] Ignore Coc on Codacy --- .codacy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.codacy.yml b/.codacy.yml index 1fba27f..0f95e8b 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -1,6 +1,7 @@ --- exclude_paths: - .github/*.md + - CODE_OF_CONDUCT.md - CHANGELOG.md - changelog/**/*.md