Hey :wave: I'm a colleague of <@U09DJMR0Z54> who'...
# permit-saas
j
Hey 👋 I'm a colleague of @Míla Votradovec who's been in touch concerning TF support. First of all thanks for solving our issues 🙏 We've got further with our TF for permit.io and would actually like to start managing our production. The best way would I think be to be able to
tf import
the current production state, but that is not supported by the provider. Is there a chance this could be implemented (I'd say I'd be useful in general). Or do you have any other suggestion how to start managing existing resources?
o
Hi Jakub thanks for reaching out and great to hear about your progress. well we agree that TF import would be a great feature to have. it is unlikely will be able to prioritize it for any immediate development anytime soon. that being said the project is an Open source One so if you'd like you can and we'd love it if you contribute that feature to the terraform. other options to consider is the CLI capabilities around terraform which allow you to export an environment to terraform. (exporting and reapplying via Terraform to a clean env)
j
Thank you for an honest answer, I understand this may not fit into your priorities. We'll try to find a reasonable solution. Clean environment was (maybe still is) considered but seemed a bit risky for us as we already have users the the current production env.
o
Environments are really built with a CI/CD cloud-native flow in mind. Imagine doing blue-green here, gradually setting up your new env and linking a PDP to it, then after testing switching the current PDP (with the old env) to a the one via DNS or load-balancer config. if you need help importing high volumes of data into an env, let us know, we can help with that
j
Thank you for an advice, sounds like this may be an option. Will discuss with colleagues on Monday 🙂
💜 1
o
wishing you a great weekend in the meanwhile
j
same to you, have a great one
s
Hey, let me join the conversation as I @Jakub Urban passed the task to implement this on me 🙂 If we create a new environment from scratch and just push TF policy there, the environment will be missing the directory of all resource ownerships and role assignments. How to work around that?
a
Hey @Sara Wachulec, we're glad you joined us, I'll let @Ask Inkeep answer this and give advices.
a
Thanks for tagging me @Alon Boshi. I'll look through relevant sources and get back to you shortly.
When migrating to a new environment using Terraform, you're right that the policy schema (resources, roles, actions) will be created, but the runtime data (users, role assignments, resource instances) won't be included since Terraform manages the schema, not the data (1) . For handling the directory data (users, role assignments, resource ownerships) , you have a few options: 1. ​*Use Permit's SDK to sync the data*​ - The Mesa Verde example shows how to programmatically sync users and role assignments (2) :
Copy code
// Add User to Tenant  
await permit.api.syncUser({  
 key: email,  
 email,  
}) ;
For role assignments at the tenant level :
Copy code
await permit.api.assignRole({  
 role: "AccountOwner",  
 tenant: cleanedEmail,  
 user: email,  
}) ;
For resource-specific role assignments :
Copy code
await permit.api.roleAssignments.assign({  
 role: "Sender",  
 tenant,  
 resource_instance: `Transaction:${transaction.id}`,  
 user,  
}) ;
2. ​*Export and import data via API*​ - You could write a script to export all current users, tenants, and role assignments from your production environment using Permit's API, then re-import them into the new environment. 3. ​*Blue-green deployment approach*​ - As suggested in the thread, you could gradually set up the new environment, test it with a PDP connected to it, then switch your production PDP to the new environment via DNS or load-balancer configuration once verified. The Terraform provider focuses on managing the authorization schema (resources, roles, permissions) as infrastructure code (1) , while the runtime data should be managed through your application's normal data flow using the SDKs (2) .
s
so there is no built in way to automatically migrate directory from one environment to another
a
To learn more, see the sources I considered: [[1] Permit.io Terraform Provider](https://docs.permit.io/integrations/infra-as-code/terraform-provider) [[2] Fintech / Banking Demo Application](https://docs.permit.io/modeling/mesa-verde)
a
@Ask Inkeep can you expand on
env copy
?
a
Thanks for tagging me @Alon Boshi. I'll look through relevant sources and get back to you shortly.
The
env copy
feature allows you to copy or merge environments, which is useful for managing policy changes in a controlled manner (1) . You can copy environments into new environments or existing environments . How it works When copying environments, only ​*Policy*​ objects are copied (Resources, Roles, Condition Sets) . ​*Directory*​ objects like Users, Tenants, and Resource Instances are NOT copied . API Usage You can copy an environment using the API :
Copy code
curl --location '<https://api.permit.io/v2/projects/{project_id}/envs/{env_id}/copy>' \  
 -H 'authorization: Bearer API_SECRET_KEY' \  
 --data '{  
 "target_env": {  
 "new": {  
 "key": "prod",  
 "name": "production"  
 }  
 }'
CLI Usage The CLI provides the
permit env copy
command (2) :
Copy code
$ permit env copy --api-key permit_key --from staging --to production --conflict-strategy overwrite
Conflict Strategy When merging into an existing environment, you can handle conflicts with (1) : • ​*fail*​ (default) : Rollback the entire merge operation • ​*overwrite*​: Overwrite the existing object in the target environment Scope Control You can include/exclude specific objects when copying :
Copy code
curl --location '<https://api.permit.io/v2/projects/{project_id}/envs/{env_id}/copy>' \  
 -H 'authorization: Bearer API_SECRET_KEY'\  
 --data '{  
 "target_env": {  
 "new": {  
 "key": "prod",  
 "name": "production"  
 }  
 },  
 "scope": {  
 "roles": {  
 "exclude": ["*"]  
 },  
 "resources": {  
 "include": ["folder", "file"]  
 },  
 "resource_sets": {  
 "exclude": ["test*"]  
 },  
 "user_sets": {  
 "exclude": ["canada_users"]  
 },  
 "custom_policies": {  
 "include": ["*.rego"]  
 }  
 }  
}'
Background API for Large Environments For large environments, you can use the async endpoint :
Copy code
curl --location '<https://api.permit.io/v2/projects/default/envs/staging/copy/async>' \  
 -H 'authorization: Bearer API_SECRET_KEY' \  
 --data '{  
 "target_env": {  
 "new": {  
 "key": "prod",  
 "name": "production"  
 }  
 }'
This is particularly useful for your migration scenario - you can copy your production policy schema to a new environment, then handle the directory data separately as discussed earlier.
To learn more, see the sources I considered: [[1] Create a new Environment](https://docs.permit.io/manage-your-account/creating-environments) [[2] Environments management using the Permit CLI](https://docs.permit.io/how-to/permit-cli/permit-cli-envs)
o
Hi @Alon Boshi please allow me to chime in, In short, currently, there is no automatic way to copy directory data (also called facts) from one env onto another; You can use the API to read your data, or your PDPs current cache - and write a script to copy that that onto your target environment.
s
thanks, our main problem is that the permit.io is currently set as the source of truth so we need the facts to be synchronized before we switch to the new env. Problem is that users are continously working on the data so as the environment copy is not an atomic operation we may get desynced in the meantime. I will have to rethink how do we want to safely approach the migration so the system would be able to recover after desync (especially for the future blue green deployments)