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

feat: add resource snowflake_user_password_policy_attachment (#2162) #2307

Conversation

raulbonet
Copy link
Contributor

@raulbonet raulbonet commented Dec 25, 2023

Description

ref #2162
This PR creates the new resource snowflake_user_password_policy_attachment. The implementation mimics the one of its brother, snowflake_account_password_policy_attachment

@raulbonet raulbonet changed the title fix: tests now do not rely on an existing user feat: Add snowflake_password_policy_attachment resource (#2162) Dec 25, 2023
@raulbonet raulbonet marked this pull request as draft December 25, 2023 16:01
@raulbonet raulbonet changed the title feat: Add snowflake_password_policy_attachment resource (#2162) feat: add resource snowflake_user_password_policy_attachment (#2162) Dec 25, 2023
@raulbonet raulbonet marked this pull request as ready for review December 25, 2023 16:33
@sfc-gh-asawicki
Copy link
Collaborator

Hey @raulbonet. Thanks for the PR.

To merge it, we require a few more things:

  • (easy) generate the documentation of the new resource (run make dev-setup && make docs);
  • (medium) add test cases or steps to the prepared test which shows update and delete behavior;
  • (medium) validation in tests that the password policy is assigned to the user (consult getRowAccessPolicyFor and policyReference in helpers_test.go);
  • (medium) implement read and import methods.

The reason for that is that publishing a partially implemented/not thoroughly tested resource will result in more issues being created. We want to avoid that.

Would you be willing to introduce these changes? Alternatively, we can merge it as a separate branch and add the missing parts ourselves (but probably not earlier than Feb-Mar).

cc: @sfc-gh-swinkler @sfc-gh-jcieslak

@raulbonet
Copy link
Contributor Author

Hello @sfc-gh-asawicki

Thanks for coming back to me. About your comments, I am happy to do all of this myself, but I want to make sure we are in the same page. As I was mentioning in the OP:

  1. The "brother" of this resource, snowflake_account_password_policy_attachment, does not implement (properly) the Read() method either (it only reads if the network policy exists, but not if it is attached). Is it a bug?

  2. I think there are several resources that do not implement the Read functionality (like a lot of the grants I believe). I thought precisely that you were merging stuff in iterations, but if it is not the case from now on, happy to fully develop it.

  3. The Read() method is not implemented because the new sdk lacks the implementation to read from POLICY REFERENCES (I think it is possibly to do it with the interfaces in snowflake, but I guess from the code that you are deprecating this). I am happy to implement it myself, but let's make sure we are in the same page:

    3.1. You would like me to create a a new interface in the sdk, let's say policy_references.go, which among other things, method mimics this: https://docs.snowflake.com/en/sql-reference/functions/policy_references ?

    3.2. If so, would you like to:
    a) Create one single interface for all kinds of policy references (masking, session, password, row access)
    b) Create a specific interface for password (so, policy_references_password.go)

@sfc-gh-asawicki
Copy link
Collaborator

Hey @raulbonet. That's great to hear that! I've included my answers below.

  1. The "brother" of this resource, snowflake_account_password_policy_attachment, does not implement (properly) the Read() method either (it only reads if the network policy exists, but not if it is attached). Is it a bug?

Maybe not a bug, but an imperfection. :) It should be implemented too, but this is low on our roadmap. As you may have noticed, the team changed in the past few months. The resource was introduced earlier in a rushed way. We follow a bit different development cycle now. In other words, we try to avoid introducing missing/untested functionalities and are slowly correcting the buggy/incomplete ones.

You can add it too, if you want. But please do it as a separate PR (we prefer PRs targeting one issue only).

  1. I think there are several resources that do not implement the Read functionality (like a lot of the grants I believe). I thought precisely that you were merging stuff in iterations, but if it is not the case from now on, happy to fully develop it.

We are merging stuff in iterations, but only in certain cases. One example would be when we introduce a new resource in the SDK. It does not change the provider's behavior and can be later used in a separate PR to migrate the resource from the old snowflake one to the new SDK client.

Here, the situation is different. An entirely new resource would be introduced, so people would start using it. And that may result in more issues being reported. We want to limit possible problems by correctly preparing the resource.

  1. The Read() method is not implemented because the new sdk lacks the implementation to read from POLICY REFERENCES (I think it is possibly to do it with the interfaces in snowflake, but I guess from the code that you are deprecating this). I am happy to implement it myself, but let's make sure we are in the same page:

It is not implemented in the SDK but was already used for testing. It is here with a TODO comment to make it a not-test-only resource:

func getRowAccessPolicyFor(t *testing.T, client *sdk.Client, id sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) (*policyReference, error) {
.

3.1. You would like me to create a a new interface in the sdk, let's say policy_references.go, which among other things, method mimics this: https://docs.snowflake.com/en/sql-reference/functions/policy_references ?

Yes. But let's limit it just to POLICY REFERENCES (the ENTITY syntax; I think we don't need the POLICY NAME one).

3.2. If so, would you like to:
a) Create one single interface for all kinds of policy references (masking, session, password, row access)
b) Create a specific interface for password (so, policy_references_password.go)

For now, you can go with 3.2.a -> e.g. PolicyReferences#GetForEntity. You can either reuse the existing implementation (we have one like that for system functions:

sql := fmt.Sprintf(`SELECT SYSTEM$GET_TAG('%s', '%s', '%v') AS "TAG"`, tagID.FullyQualifiedName(), objectID.FullyQualifiedName(), objectType)
) but preferably we would like to stick with the syntax using opts and request (like CreateDatabaseRoleRequest and createDatabaseRoleOptions). We will accept either way.

We tend to add unit tests (like TestDatabaseRoleCreate) and validations (like func (opts *createDatabaseRoleOptions) validate()). Also, we add integration tests (like in database_role_integration_test.go) that validate the SDK in separation from Terraform (one day, we will extract the SDK as a separate project).

If you have any more questions (or more questions will appear during the implementation), feel free to ask them!

And so you know: we are aware that we lack clear contributing guidelines. We are currently working on transparency, so we should have an updated contributing guide ready in the upcoming weeks.

@raulbonet raulbonet marked this pull request as draft January 3, 2024 03:46
@sfc-gh-asawicki
Copy link
Collaborator

@raulbonet, thanks for all the changes you made after our reviews! It's very helpful for us to receive help with the implementation.

Before we proceed, please answer my comment: #2307 (comment), the part about unit and integration tests. We can write or help you write them, but we need a consensus here because we can only merge the change with them.

@raulbonet
Copy link
Contributor Author

Hello @sfc-gh-asawicki
Sorry, yesterday night I made the changes and did not answer your message.

  1. About the computed field issue: I think I will create a parallel branch and re-test the behaviour because I think that the behaviour was incorrect, but I might be wrong: even when you changed the database or schema of the Password Policy (which triggers a replacement of the PP), this did not in turn trigger a replacement of the attachment, but I am going to double check.

  2. About unit test: I can add them.

  3. About integration tests: we would love to collaborate but this is a bit more delicate: unfortunately, I am seeing that the integration tests (and acceptance tests) might leave some "resource traces" in our Snowflake account (warehouses, users, etc.), particularly if they fail, so we do not feel comfortable running these in our Snowflake account. We requested an extra Snowflake Account to our Account manager but we did not get an answer; until we have this sorted out we prefer not to run these. Would you mind chiming in for the integration tests?

},
},
}
assertOptsValidAndSQLEquals(t, opts, "SELECT * FROM TABLE (SNOWFLAKE.INFORMATION_SCHEMA.POLICY_REFERENCES (ref_entity_name => '%s', ref_entity_domain => 'user'))", strings.ReplaceAll(userName.FullyQualifiedName(), `"`, `\"`))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

About the delimiters: the sqlBuilder transforms the " into \" . I guess the problem comes from the workaround we used because the builder does not support both single quotations and double quotations at the same time: so, the sqlBuilder interprets the quotes " as a string, and prefixes them with a backslash.

I solved it with this workaround you see:

strings.ReplaceAll(userName.FullyQualifiedName(), `"`, `\"`)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems fine, we will check the statement execution with the integration tests and change the sqlbuilder behavior or opts config if needed.

pkg/sdk/policy_references_test.go Show resolved Hide resolved
sfc-gh-asawicki
sfc-gh-asawicki previously approved these changes Feb 8, 2024
Copy link

gitguardian bot commented Feb 8, 2024

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
- Generic Password 25e6a2c pkg/resources/managed_account_test.go View secret
- Generic Password 25e6a2c pkg/resources/managed_account_test.go View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

@sfc-gh-asawicki
Copy link
Collaborator

/ok-to-test sha=25e6a2c76a138bc3d4b58d6d959d713192d1a7d3

@sfc-gh-asawicki
Copy link
Collaborator

@raulbonet Thanks for the contribution one more time!

We decided to merge it and add the integration tests on our side. If we uncover any problems with the generated statements we will just fix them. Merging without them should not affect anyone because we plan the next release for Wednesday/Thursday next week. We may also adjust the resource schema to what we discussed (single fully qualified id instead of 3 parts but it is yet to be decided).

Copy link

github-actions bot commented Feb 8, 2024

Integration tests failure for 25e6a2c76a138bc3d4b58d6d959d713192d1a7d3

@sfc-gh-asawicki sfc-gh-asawicki merged commit 93af462 into Snowflake-Labs:main Feb 8, 2024
6 of 8 checks passed
sfc-gh-jcieslak pushed a commit that referenced this pull request Feb 15, 2024
🤖 I have created a release *beep* *boop*
---


# Release notes
[0.86.0](v0.85.0...v0.86.0)
(2024-02-15)


## 🎉 **What's new**

* add refresh_mode and initialize to dynamic tables
([#2437](#2437))
([d301b20](d301b20))
* add resource snowflake_user_password_policy_attachment
([#2162](#2162))
([#2307](#2307))
([93af462](93af462))
* create a workaround for granting privileges on all pipes
([#2477](#2477))
([64f2346](64f2346))
* Handle IMPORTED PRIVILEGES privileges in privilege-to-role granting
resources
([#2471](#2471))
([eb20051](eb20051))
* use external functions
([#2454](#2454))
([417d473](417d473))
* use funcs from sdk
([#2462](#2462))
([a5f969c](a5f969c))
* use sdk for procedures
([#2450](#2450))
([94ac78a](94ac78a))
* Use sdk in table constraint resource
([#2466](#2466))
([d685603](d685603))
* Use tables from SDK
([#2453](#2453))
([fdb4f88](fdb4f88))


## 🔧 **Misc**

* Add migration notes to the docs and change jira integration
([#2497](#2497))
([b17f1af](b17f1af))
* Change email and issue reporter
([#2470](#2470))
([5865655](5865655))
* Grants migration guide
([#2455](#2455))
([62c70fd](62c70fd))
* Remove unused old implementation from snowflake pkg
([#2458](#2458))
([2d0e508](2d0e508))
* update password policy attachment
([#2485](#2485))
([6ec9ff7](6ec9ff7))


## 🐛 **Bug fixes**

* allow DT warehouse to be updated in-place
([#2439](#2439))
([d565af1](d565af1))
* correct test dependencies
([#2493](#2493))
([dfb247f](dfb247f))
* FileFormat not detecting changes correctly
([#2436](#2436))
([018bb74](018bb74))
* Fix few smaller issues
([#2507](#2507))
([a836871](a836871))
* Fix functions and small other fixes
([#2503](#2503))
([0d4aba4](0d4aba4)),
closes
[#2490](#2490)
* Fix tag tests in view and in materialized view
([#2457](#2457))
([2de942a](2de942a))
* Fix task related issues
([#2479](#2479))
([0385650](0385650))
* Fix tests that base on default data retention
([#2465](#2465))
([682e28c](682e28c))
* grant privileges to share test terraform dependencies
([#2473](#2473))
([ede8d95](ede8d95))
* parameter issues
([#2463](#2463))
([7ee4986](7ee4986))
* parse dynamic table query from DDL
([#2438](#2438))
([d76815c](d76815c))
* Remove title and body temporarily from jira integration
([#2499](#2499))
([672c97d](672c97d))
* SHOW GRANTS mapping for share data type
([#2508](#2508))
([feb4d44](feb4d44))
* user error handling
([#2486](#2486))
([dfa52b2](dfa52b2))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: snowflake-release-please[bot] <105954990+snowflake-release-please[bot]@users.noreply.github.com>
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.

3 participants