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

Allow P2F custom templates via ConfigMap #393

Merged
merged 3 commits into from
Dec 6, 2021
Merged

Allow P2F custom templates via ConfigMap #393

merged 3 commits into from
Dec 6, 2021

Conversation

john-odonnell
Copy link
Contributor

@john-odonnell john-odonnell commented Nov 22, 2021

Desired Outcome

From ONYX-14093:

Currently custom templates must be provided through the conjur.org/secret-file-template.{secret-group} annotation. We would also like to be able to provide a template through a configmap such that the configmap is mounted as a volume and secrets-provider reads the template as a file from the file system.

The template file should be mounted to a hard-coded path, and must use the file name {secret-group}.tpl. Add documentation to PUSH_TO_FILE.md that describes this path.

Secrets Provider must be told to look for a mounted ConfigMap template. There are a couple of options here:

  • adding a new annotation which indicates that a secret group is used a template mounted from a ConfigMap
  • adding a new "template" file format enum for the conjur.org/secret-file-format.{secret-group} annotation
    Providing both the template filepath and annotation should result in a fatal error.

Implemented Changes

Behavioral Changes

If a secret group's output file format, specified with the annotation conjur.org/secret-file-format.{secret-group}, is set to template, then the function newSecretGroup will look for either an annotation- or ConfigMap-based template string.

The ConfigMap defining templates must be mounted to /conjur/templates, and the key for a given template value must be {secret-group}.tpl. At secret group creation, Secrets Provider will only attempt to read from this file. If the file does not exist, it is assumed that a template was not provided via ConfigMap.

If the annotation conjur.org/secret-file-format.{secret-group} is set to template, and templates are provided through BOTH the group's annotation and volume-mounted ConfigMap, secret group creation will receive a failing error.

If the annotation conjur.org/secret-file-format.{secret-group} is not set to template, Secrets Provider will never look for a template in either form. If a template is provided by either/both method(s), it will be ignored.

Explicit Changes

  • Added template to conjur.org/secret-file-format.{secret-group} accepted values
  • Added pkg/secrets/pushtofile/pull_from_reader.go, a counterpart to push_to_writer.go, to mock and dependency-inject reading content from the filesystem.
  • In secret_group.go:
    • Separated func NewSecretGroups into itself and func newSecretGroupsWithDeps to dep-inject file reading operations
    • Added new functions collectTemplate and readTemplateFromFile
  • Updated unit tests to reflect the above changes
  • PUSH_TO_FILE.md updates

Connected Issue/Story

Resolves #[relevant GitHub issue(s), e.g. 76]

CyberArk internal issue link: ONYX-14903

Definition of Done

At least 1 todo must be completed in the sections below for the PR to be
merged.

Changelog

  • The CHANGELOG has been updated, or
  • This PR does not include user-facing changes and doesn't require a
    CHANGELOG update

Test coverage

  • This PR includes new unit and integration tests to go with the code
    changes, or
  • The changes in this PR do not require tests

Documentation

  • Docs (e.g. READMEs) were updated in this PR
  • A follow-up issue to update official docs has been filed here: insert issue ID
  • This PR does not require updating any documentation

Behavior

  • This PR changes product behavior and has been reviewed by a PO, or
  • These changes are part of a larger initiative that will be reviewed later, or
  • No behavior was changed with this PR

Security

  • Security architect has reviewed the changes in this PR,
  • These changes are part of a larger initiative with a separate security review, or
  • There are no security aspects to these changes

@john-odonnell john-odonnell force-pushed the ONYX-14093 branch 2 times, most recently from 19a36d8 to 727bc0d Compare November 29, 2021 18:14
@@ -297,16 +323,25 @@ func NewSecretGroups(secretsBasePath string, annotations map[string]string) ([]*
return sgs, nil
}

func newSecretGroup(groupName string, secretsBasePath string, annotations map[string]string) (*SecretGroup, []error) {
func newSecretGroup(groupName string, annotations map[string]string, c Config) (*SecretGroup, []error) {
Copy link

Choose a reason for hiding this comment

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

Function newSecretGroup has 5 return statements (exceeds 4 allowed).

if err != nil {
return nil, []error{err}
}

return sg, nil
}

func collectTemplate(groupName string, annotations map[string]string, c Config) (string, error) {
Copy link

Choose a reason for hiding this comment

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

Function collectTemplate has 5 return statements (exceeds 4 allowed).

@john-odonnell john-odonnell changed the title WIP: Allow P2F custom templates via ConfigMap Allow P2F custom templates via ConfigMap Nov 29, 2021
@john-odonnell john-odonnell marked this pull request as ready for review November 29, 2021 19:59
@john-odonnell john-odonnell requested review from a team as code owners November 29, 2021 19:59
Copy link
Contributor

@diverdane diverdane left a comment

Choose a reason for hiding this comment

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

Looks great!!! I'm thinking we should ignore the warnings about 5 returns in 2 functions. That seems too restrictive for Go.

I just have one question on whether UT covers custom template not being defined by either Annotation or ConfigMap.

})
})

t.Run("custom template file - annotation and configmap template provided", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this UT test case for custom template being provided by both annotation and configmap.

Is there a corresponding UT test case for secret file format being template, but no custom template defined by either annotation or configmap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just added a test case for the above context.

@codeclimate
Copy link

codeclimate bot commented Dec 3, 2021

Code Climate has analyzed commit 4d0dcf8 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

The test coverage on the diff in this pull request is 94.2% (50% is the threshold).

This pull request will bring the total coverage in the repository to 92.9% (0.1% change).

View more on Code Climate.

Copy link
Contributor

@diverdane diverdane left a comment

Choose a reason for hiding this comment

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

LGTM!!! I don't know if there's a way to add a repository-specific CodeClimate config that would allow more then 4 returns for Golang functions. If not, I think we should just ignore the two instances of CodeClimate errors for 5 returns in a function.

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