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

API: Enhance permissions to allow filtering by environments #260

Open
rajdip-b opened this issue Jun 6, 2024 · 11 comments
Open

API: Enhance permissions to allow filtering by environments #260

rajdip-b opened this issue Jun 6, 2024 · 11 comments
Labels
difficulty: 5 foss hack Clustering all the curated issues for Foss Hack 2024 help wanted Extra attention is needed priority: high scope: api Everything related to the API type: enhancement New feature or request

Comments

@rajdip-b
Copy link
Member

rajdip-b commented Jun 6, 2024

Description

Right now, the roles that are created can have one or many projects associated with it. Associating projects mean that members who will have this role attached to them, will be able to perform the authorities in WorkspaceRole entity [refer to prisma schema].

We would like to have another layer of security in here. We would also like to introduce environment specific access, so that members can have access to only a specific set of environments in the project set by the admin.

Use case:

Consider that there's a project that has 3 environments - dev, stage and prod. It will be ideal to allow the developers access the dev and stage environments (and the secrets and variables in it) while the prod environment is only accessible by the DevOps team or such. This is where this feature will be helpful

Solution

  • Update the ProjectWorkspaceRoleAssociation entity to include environments - specifying the environments accessible by the member.
  • Create a reverse relation from Environment on to ProjectWorkspaceRoleAssociation.
  • Add a util function named getCollectiveEnvironmentAuthorities. You can take the reference from any get-collective-project-authorities.ts. The purpose of this function would be this: given the userId, project, and environment, it would need to fetch all the authorities that the member has over this environment.
  • Update AuthorityCheckerService#checkAuthorityOverEnvironment to incorporate the getCollectiveEnvironmentAuthorities function and filter the roles.
  • Update the projectIds to be a of type Map<String, Array<String>>. The association will be something like this: For every projectId, list of environmentIds that will be available to the role.
  • Update the WorkspaceRoleService functions acordingly
  • Update the required tests in workspace-role module.
@rajdip-b rajdip-b added type: enhancement New feature or request help wanted Extra attention is needed scope: api Everything related to the API priority: high labels Jun 6, 2024
@rajdip-b rajdip-b moved this to Todo in keyshade-api Jun 6, 2024
@rajdip-b rajdip-b added difficulty: 5 foss hack Clustering all the curated issues for Foss Hack 2024 labels Jun 6, 2024
@rayaanoidPrime
Copy link
Contributor

/attempt

Copy link

Assigned the issue to you!

@rajdip-b rajdip-b moved this from Todo to In progress in keyshade-api Jun 11, 2024
@rajdip-b
Copy link
Member Author

Hey @rayaanoidPrime! Could you hold up to this for a day or two? I'm making a major refactor to the way secrets, variables and environments are organized. I would notify you once its done.

@rayaanoidPrime
Copy link
Contributor

Oh sure no worries

@rajdip-b
Copy link
Member Author

Hey bro! @rayaanoidPrime

v2.0.0 just got released! You can start working now. Do hit me up if you want help.

@rajdip-b
Copy link
Member Author

rajdip-b commented Sep 5, 2024

Howdy @rayaanoidPrime, any updates yet?

@rayaanoidPrime
Copy link
Contributor

Hey @rajdip-b sorry for being unresponsive just been kinda busy last few months. I remember being done with it and I think only the tests are left to be written. Let me see if I can do a pr this afternoon.

@rajdip-b
Copy link
Member Author

rajdip-b commented Sep 5, 2024

No rush bro! Just let us know when you can put up the PR.

@rayaanoidPrime
Copy link
Contributor

Hi! In light of the evolutions and additions that the code has undergone in the time after this issue was posted I have thought of a few changes to the initial solution format. (the core logic remains the same ). Let me know your thoughts

  1. Update CreateWorkspaceRole DTO:
class CreateWorkspaceRole {
  // ... other properties ...
  @IsOptional()
  @ValidateNested({ each: true })
  @Type(() => ProjectEnvironments)
  readonly projectEnvironments?: ProjectEnvironments[]
}
class ProjectEnvironments {
  @IsString()
  projectSlug: string
  @IsArray()
  @IsString({ each: true })
  environmentSlugs: string[]
}
  1. Modify WorkspaceRoleService :: createWorkspaceRole and updateWorkspaceRole methods to handle new project-environment associations.
  2. Revise workspace-role.e2e.spec.ts:
  • Update existing tests and add new ones to cover the new functionality.
  • Create actual environments (Dev and Prod) for each project in the test setup.
  • Use these created environments in the tests instead of undefined environment names.
  1. Update checkAuthorityOverEnvironment to incorporate the getCollectiveEnvironmentAuthorities function and filter the roles.
  2. Add new helper function in WorkspaceService:
    • Create a new private method getEnvironmentSlugToIdMap to fetch environment IDs:
    private async getEnvironmentSlugToIdMap(environmentSlugs: string[]) {
      const environments = await this.prisma.environment.findMany({
        where: {
          slug: {
            in: environmentSlugs
          }
        }
      })
      return new Map(environments.map((env) => [env.slug, env.id]))
    }
    This function takes an array of environment slugs, queries the database to find matching environments, and returns a Map with environment slugs as keys and their corresponding IDs as values.

@rajdip-b
Copy link
Member Author

Yes! I believe that's what needs to be done. Thanks for the detailed writeup.

@rajdip-b
Copy link
Member Author

rajdip-b commented Nov 5, 2024

@rayaanoidPrime hey bro long time no see. I'm unassigning this for now. Feel free to pick this up later on.

@rajdip-b rajdip-b moved this from In progress to Todo in keyshade-api Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: 5 foss hack Clustering all the curated issues for Foss Hack 2024 help wanted Extra attention is needed priority: high scope: api Everything related to the API type: enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

2 participants