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

Support managing users #148

Merged
merged 13 commits into from Feb 15, 2023
Merged

Support managing users #148

merged 13 commits into from Feb 15, 2023

Conversation

merkata
Copy link
Contributor

@merkata merkata commented Feb 10, 2023

This is a PR that enables managing users (add, remove). It closes #143. It updates on a password change only as changing the display name does not correspond to a juju API call. To be consistent with the juju CLI, it won't manipulate model access here, that would be done with granting additional permission in #144 .

@merkata merkata marked this pull request as ready for review February 12, 2023 21:56
@juanmanuel-tirado
Copy link
Contributor

Could you add some TF plans and use cases to test this manually?

@mthaddon
Copy link

One question here @juanmanuel-tirado do you think it might be better for us to disable users rather than deleting them, given that deleting a user in Juju doesn't actually delete them, but means you can no longer use that username? Disabling them would mean it's a reversible operation, so would be safer, but possibly less expected.

@juanmanuel-tirado
Copy link
Contributor

That's a good question. Following the CRUD policy used in terraform I will go with the remove-users as it is right now. However, it is true that we can find some problems if a user is removed by mistake. This is something that should be addressed on the Juju side rather than here.

@merkata
Copy link
Contributor Author

merkata commented Feb 14, 2023

Reproducing the change locally:

  • populate ~/.terraformrc with:
plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"
disable_checkpoint = true
  • prepare a plugin directory with mkdir -p ~/.terraform.d/plugins/merkata.com/juju/juju/0.3.1/linux_amd64/
  • install the provider via go install
  • copy the provider cp $HOME/go/bin/terraform-provider-juju ~/.terraform.d/plugins/merkata.com/juju/juju/0.3.1/linux_amd64/
  • prepare a main.tf:
terraform {
  required_providers {
    juju = {
      version = "~> 0.3.1"
      source  = "merkata.com/juju/juju"
    }
  }
}

provider "juju" {}

resource "juju_user" "this" {
  name         = "user-test"
  display_name = format("%s - terraform managed", "user-test")
  password     = var.password
}
  • prepare a variables.tf:
variable "password" {
  sensitive = true
  type      = string
}
  • initialize provider via terraform init
  • optionally set password with export TF_VAR_password=<some password>
  • run terraform plan
Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # juju_user.this will be created
  + resource "juju_user" "this" {
      + display_name = "user-test - terraform managed"
      + id           = (known after apply)
      + name         = "user-test"
      + password     = (sensitive)
    }

Plan: 1 to add, 0 to change, 0 to destroy.
  • run terraform apply
Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # juju_user.this will be created
  + resource "juju_user" "this" {
      + display_name = "user-test - terraform managed"
      + id           = (known after apply)
      + name         = "user-test"
      + password     = (sensitive)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

juju_user.this: Creating...
juju_user.this: Creation complete after 0s [id=user-test]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  • verify the user is created with juju users
Controller: mk8s

Name       Display name                   Access     Date created    Last connection
admin*     admin                          superuser  2023-02-08      just now
user-test  user-test - terraform managed  login      37 seconds ago  never connected
  • logout as admin and login with the new user juju login -u user-test
please enter password for user-test on mk8s: 
Welcome, user-test. You are now logged into "mk8s".

There are no models available. You can add models with
"juju add-model", or you can ask an administrator or owner
of a model to grant access to that model with "juju grant".
  • change the password for the user and run terraform plan
juju_user.this: Refreshing state... [id=user-test]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # juju_user.this will be updated in-place
  ~ resource "juju_user" "this" {
        id           = "user-test"
        name         = "user-test"
      ~ password     = (sensitive)
        # (1 unchanged attribute hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
  • run terraform apply
juju_user.this: Refreshing state... [id=user-test]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # juju_user.this will be updated in-place
  ~ resource "juju_user" "this" {
        id           = "user-test"
        name         = "user-test"
      ~ password     = (sensitive)
        # (1 unchanged attribute hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

juju_user.this: Modifying... [id=user-test]
juju_user.this: Modifications complete after 0s [id=user-test]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
  • logout and login again as the user with the new password
please enter password for user-test on mk8s: 
Welcome, user-test. You are now logged into "mk8s".

There are no models available. You can add models with
"juju add-model", or you can ask an administrator or owner
of a model to grant access to that model with "juju grant".
  • cleanup with terraform destroy
juju_user.this: Refreshing state... [id=user-test]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # juju_user.this will be destroyed
  - resource "juju_user" "this" {
      - display_name = "user-test - terraform managed" -> null
      - id           = "user-test" -> null
      - name         = "user-test" -> null
      - password     = (sensitive) -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

juju_user.this: Destroying... [id=user-test]
juju_user.this: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.
  • logout and verify the user is not present in juju via juju login -u user-test
please enter password for user-test on mk8s: 
ERROR cannot log into controller "mk8s": cannot get discharge from "https://localhost:33643/auth": cannot submit form: Post https://localhost:33643/auth/form: user "user-test" is permanently deleted

Copy link
Contributor

@juanmanuel-tirado juanmanuel-tirado left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

I think the solution is perfectly valid. I just made some comments on how to set the Id of the resource. Could we please for the sake of completeness add some manual steps for QA? Something like in #149
I already saw the manual QA. Thanks.

internal/provider/resource_user.go Outdated Show resolved Hide resolved
internal/provider/resource_user.go Outdated Show resolved Hide resolved
internal/provider/resource_user.go Outdated Show resolved Hide resolved
@mthaddon
Copy link

That's a good question. Following the CRUD policy used in terraform I will go with the remove-users as it is right now. However, it is true that we can find some problems if a user is removed by mistake. This is something that should be addressed on the Juju side rather than here.

I've filed https://bugs.launchpad.net/juju/+bug/2007258 about this.

Copy link
Contributor

@juanmanuel-tirado juanmanuel-tirado left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

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.

Support managing users
3 participants