Am I able to set policies by role / resource using...
# permit-saas
a
Am I able to set policies by role / resource using OpenAPI / yaml and then syncing via the CLI / SDK? Or do I need to use the UI?
a
Hi @Alec Handal, I'm an AI assistant for Permit.io. I'll analyze available sources and get back to you shortly.
Yes, you can set policies by role/resource using OpenAPI/YAML and sync via the CLI without needing to use the UI. The Permit CLI provides the
env apply openapi
command specifically for this purpose. (1) (2) Using OpenAPI to Define Policies The command creates a full policy schema in Permit by reading an OpenAPI spec file and using
-x-permit
extensions. (1) You can run it with:
Copy code
permit env apply openapi --spec-file ./api-spec.json
(1) (2) Or with a remote file:
Copy code
permit env apply openapi --key permit_key_... --spec-file <https://raw.githubusercontent.com/daveads/openapispec/main/blog-api.json>
OpenAPI Extensions for Policy Definition You define policies using these
-x-permit
extensions in your OpenAPI spec: ​*Path Level:*​ •
x-permit-resource
- The resource name (required) ​*Operation Level (HTTP Method) :*​ •
x-permit-action
- Action name (defaults to HTTP method if not provided) •
x-permit-role
- Top-level role allowed for the operation •
x-permit-resource-role
- Resource-level role allowed for the operation Here's an example OpenAPI spec with Permit extensions:
Copy code
openapi: 3.0.3
info:
  title: 'Blog API with Permit Extensions'
  version: '1.0.0'
paths:
  /posts:
    x-permit-resource: blog_post
    get:
      summary: List all posts
      x-permit-action: list
      x-permit-role: viewer
    post:
      summary: Create a new post
      x-permit-action: create
      x-permit-role: editor
      x-permit-resource-role: post_creator
  /posts/{postId}:
    x-permit-resource: blog_post
    delete:
      summary: Delete a post
      x-permit-action: delete
      x-permit-role: admin
The CLI will automatically create URL mappings in Permit that connect API endpoints to the appropriate resources and actions for runtime authorization checks. (1)
To learn more, see the sources I considered: [[1] Policy - AI, Templates, OpenAPI](https://docs.permit.io/how-to/permit-cli/permit-cli-policy) [[2] Permit CLI](https://github.com/permitio/permit-cli/blob/main/README.md)
o
Alec, I understand you have a zero docs reading philosophy? 😅 @Ask Inkeep please also share the API spec reference, Terraform provider, SDKs for policy creation
a
Thanks for tagging me @Or Weis. I'll look through relevant sources and get back to you shortly.
API Spec Reference The Permit.io Cloud API uses OpenAPI specifications. (1) The API docs are available at https://api.permit.io/v2/redoc and the OpenAPI configuration spec is available at https://api.permit.io/v2/openapi.json Terraform Provider The Permit Terraform Provider allows you to manage your policy schemas as code. (2) (3) Here's how to use it:
Copy code
terraform {  
 required_providers {  
 permitio = {  
 source = "<http://registry.terraform.io/permitio/permit-io|registry.terraform.io/permitio/permit-io>"  
 version = "~> 0.0.14" ///# Use the latest version  
 }  
 }  
}  
  
///# Configure the <http://Permit.io|Permit.io> provider  

provider "permitio" {  
 api_key = "YOUR_API_KEY" ///# Replace with your actual API key  
 ///# api_url = "<https://api.permit.io>" # Optional: defaults to <https://api.permit.io>  
}  
  
///# Define a resource (e.g., documents in your application)  
resource "permitio_resource" "document" {  
 key = "document"  
 name = "Document"  
 description = "A confidential document that users can read and write"  
   
 actions = {  
 "read" = {  
 "name" = "Read"  
 "description" = "Read a document"  
 }  
 "write" = {  
 "name" = "Write"  
 "description" = "Write to a document"  
 }  
 "delete" = {  
 "name" = "Delete"  
 "description" = "Delete a document"  
 }  
 }  
   
 attributes = {  
 "title" = {  
 "description" = "The title of the document"  
 "type" = "string"  
 }  
 "owner" = {  
 "description" = "The owner of the document"  
 "type" = "string"  
 }  
 }  
}  
  
///# Define roles with specific permissions  
resource "permitio_role" "reader" {  
 key = "reader"  
 name = "Reader"  
 description = "Can read documents but cannot modify them"  
 permissions = ["document:read"]  
   
 depends_on = [permitio_resource.document]  
}
(3) The Terraform Provider enables developers to configure and version policy schemas with Terraform scripts, integrating policy management directly into their software development lifecycle (SDLC) . (2) You can version policies, manage branches, and integrate them seamlessly with your CI/CD pipeline. SDKs for Policy Creation According to the SDK Feature Parity documentation, Permit provides SDKs for NodeJS, Go, .NET, Java, Python, Ruby, and Terraform. (4) The SDKs support various policy management features including: • Users • Resources • Tenants • Projects • Environments • Roles • Role Assignments • Condition Sets • Condition Set Rules • Relationship Tuples • Resource Relations • Role Derivations • Resource Instances • Resource Roles • Resource Actions • Resource Action Groups • Resource Attributes The management and synchronization of data can be handled through API/SDK integration, which is the most straightforward method for syncing data by integrating SDKs or direct API calls from within the application code. (9)
To learn more, see the sources I considered: [[1] API Reference](https://docs.permit.io/api/api-reference) [[2] Permit.io Launch Week - Day 3: Fine-Grained Modeling](https://www.permit.io/blog/launch-week-3-fine-grained-modeling) [[3] Permit.io Terraform Provider](https://docs.permit.io/integrations/infra-as-code/terraform-provider) [[4] SDK Feature Parity](https://docs.permit.io/sdk/sdks-overview)
a
@Or Weis sorry! i've read through the policy basics and gitops overview pages a bunch of times, just been running into issues with my policies not syncing properly.. didn't mean to wake the team up, your agent is just really helpful
o
I'm just teasing, it's 100% okay to ask any question you want 😇