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: Add pagination metadata to Secret module #341

Closed
rajdip-b opened this issue Jul 10, 2024 · 2 comments · Fixed by #389
Closed

API: Add pagination metadata to Secret module #341

rajdip-b opened this issue Jul 10, 2024 · 2 comments · Fixed by #389
Assignees
Labels
difficulty: 1 foss hack Clustering all the curated issues for Foss Hack 2024 good first issue Good for newcomers 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

Description

Refer to #336

Scope of change

  • async getAllSecretsOfProject(
    user: User,
    projectId: Project['id'],
    decryptValue: boolean,
    page: number,
    limit: number,
    sort: string,
    order: string,
    search: string
    ) {
    // Fetch the project
    const project =
    await this.authorityCheckerService.checkAuthorityOverProject({
    userId: user.id,
    entity: { id: projectId },
    authority: Authority.READ_SECRET,
    prisma: this.prisma
    })
    // Check if the secret values can be decrypted
    await this.checkAutoDecrypt(decryptValue, project)
    const secrets = await this.prisma.secret.findMany({
    where: {
    projectId,
    name: {
    contains: search
    }
    },
    include: {
    lastUpdatedBy: {
    select: {
    id: true,
    name: true
    }
    }
    },
    skip: page * limit,
    take: limit,
    orderBy: {
    [sort]: order
    }
    })
    const secretsWithEnvironmentalValues = new Map<
    Secret['id'],
    {
    secret: Secret
    values: {
    environment: {
    name: Environment['name']
    id: Environment['id']
    }
    value: SecretVersion['value']
    version: SecretVersion['version']
    }[]
    }
    >()
    // Find all the environments for this project
    const environments = await this.prisma.environment.findMany({
    where: {
    projectId
    }
    })
    const environmentIds = new Map(
    environments.map((env) => [env.id, env.name])
    )
    for (const secret of secrets) {
    // Make a copy of the environment IDs
    const envIds = new Map(environmentIds)
    let iterations = envIds.size
    // Find the latest version for each environment
    while (iterations--) {
    const latestVersion = await this.prisma.secretVersion.findFirst({
    where: {
    secretId: secret.id,
    environmentId: {
    in: Array.from(envIds.keys())
    }
    },
    orderBy: {
    version: 'desc'
    }
    })
    if (!latestVersion) continue
    if (secretsWithEnvironmentalValues.has(secret.id)) {
    secretsWithEnvironmentalValues.get(secret.id).values.push({
    environment: {
    id: latestVersion.environmentId,
    name: envIds.get(latestVersion.environmentId)
    },
    value: decryptValue
    ? await decrypt(project.privateKey, latestVersion.value)
    : latestVersion.value,
    version: latestVersion.version
    })
    } else {
    secretsWithEnvironmentalValues.set(secret.id, {
    secret,
    values: [
    {
    environment: {
    id: latestVersion.environmentId,
    name: envIds.get(latestVersion.environmentId)
    },
    value: decryptValue
    ? await decrypt(project.privateKey, latestVersion.value)
    : latestVersion.value,
    version: latestVersion.version
    }
    ]
    })
    }
    envIds.delete(latestVersion.environmentId)
    }
    }
    return Array.from(secretsWithEnvironmentalValues.values())
    }
@rajdip-b rajdip-b added type: enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed scope: api Everything related to the API priority: high foss hack Clustering all the curated issues for Foss Hack 2024 difficulty: 1 labels Jul 10, 2024
@rajdip-b rajdip-b moved this to Todo in keyshade-api Jul 10, 2024
@muntaxir4
Copy link
Contributor

/attempt I am attempting it's parent issue #336

Copy link

Assigned the issue to @muntaxir4!

@github-project-automation github-project-automation bot moved this from Todo to Done in keyshade-api Jul 29, 2024
@rajdip-b rajdip-b moved this from Done to Queued for release in keyshade-api Jul 29, 2024
@rajdip-b rajdip-b moved this from Queued for release to Done in keyshade-api Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: 1 foss hack Clustering all the curated issues for Foss Hack 2024 good first issue Good for newcomers help wanted Extra attention is needed priority: high scope: api Everything related to the API type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants