Skip to content

Commit

Permalink
secret manager added
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Aug 11, 2024
1 parent 647ce5c commit fb0d526
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
50 changes: 50 additions & 0 deletions add_secret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { listRepoRemote } = require("./list_repo");

const sodium = require("libsodium-wrappers");

async function encryptSecret(publicKey, secret) {
await sodium.ready;

const binkey = sodium.from_base64(publicKey, sodium.base64_variants.ORIGINAL);
const binsec = sodium.from_string(secret);

const encrypted = sodium.crypto_box_seal(binsec, binkey);

return sodium.to_base64(encrypted, sodium.base64_variants.ORIGINAL);
}

async function addSecret(owner, repoName) {
const { Octokit } = await import("@octokit/rest");
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const {
actions: { createOrUpdateRepoSecret, getRepoPublicKey },
} = octokit;

const {
data: { key_id, key },
} = await getRepoPublicKey({ owner: owner, repo: repoName });

const secretName = "META_UPDATE_KEY";
const secret = await encryptSecret(key, process.env.META_UPDATE_KEY);

createOrUpdateRepoSecret({
owner: owner,
repo: repoName,
secret_name: secretName,
encrypted_value: secret,
key_id: key_id,
});
}

async function main() {
const groupedRepolists = await listRepoRemote();

Object.keys(groupedRepolists).forEach((username) => {
groupedRepolists[username].forEach((repoName) => {
addSecret(username, repoName);
});
});
}

main();
26 changes: 14 additions & 12 deletions list_repo.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { Octokit } from "octokit";

const octakit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const {
repos: { listForAuthenticatedUser },
} = octakit.rest;

function _makeGroupByOwner(data) {
const result = {};

data.forEach((repo) => {
const owner = repo.owner.login;

if (result[owner] === null) {
if (result[owner] === undefined) {
result[owner] = [];
}

Expand All @@ -22,7 +14,15 @@ function _makeGroupByOwner(data) {
return result;
}

export async function listRepoRemote() {
async function listRepoRemote() {
const { Octokit } = await import("@octokit/rest");

const octakit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const {
repos: { listForAuthenticatedUser },
} = octakit.rest;

const { data } = await listForAuthenticatedUser({
username: "iamspdarsan",
type: "all",
Expand All @@ -32,13 +32,13 @@ export async function listRepoRemote() {
return _makeGroupByOwner(data);
}

export function listRepoLocal() {
function listRepoLocal() {
const { data } = JSON.parse(readFileSync("out.json", { encoding: "utf8" }));

return _makeGroupByOwner(data);
}

export function dumpToLocal() {
function dumpToLocal() {
listRepoRemote()
.then((content) => {
writeFileSync("out.json", JSON.stringify(content, null, 2), {
Expand All @@ -47,3 +47,5 @@ export function dumpToLocal() {
})
.catch(console.error);
}

module.exports = { listRepoRemote, listRepoLocal, dumpToLocal };
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "github-admin",
"repository": "https://github.com/darsan-in/Github-Admin.git",
"author": "PRIYADARSAN S <[email protected]>",
"devDependencies": {
"@octokit/rest": "^21.0.1",
"libsodium-wrappers": "^0.7.14"
}
}
118 changes: 118 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@octokit/auth-token@^5.0.0":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-5.1.1.tgz#3bbfe905111332a17f72d80bd0b51a3e2fa2cf07"
integrity sha512-rh3G3wDO8J9wSjfI436JUKzHIxq8NaiL0tVeB2aXmG6p/9859aUOAjA9pmSPNGGZxfwmaJ9ozOJImuNVJdpvbA==

"@octokit/core@^6.1.2":
version "6.1.2"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-6.1.2.tgz#20442d0a97c411612da206411e356014d1d1bd17"
integrity sha512-hEb7Ma4cGJGEUNOAVmyfdB/3WirWMg5hDuNFVejGEDFqupeOysLc2sG6HJxY2etBp5YQu5Wtxwi020jS9xlUwg==
dependencies:
"@octokit/auth-token" "^5.0.0"
"@octokit/graphql" "^8.0.0"
"@octokit/request" "^9.0.0"
"@octokit/request-error" "^6.0.1"
"@octokit/types" "^13.0.0"
before-after-hook "^3.0.2"
universal-user-agent "^7.0.0"

"@octokit/endpoint@^10.0.0":
version "10.1.1"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-10.1.1.tgz#1a9694e7aef6aa9d854dc78dd062945945869bcc"
integrity sha512-JYjh5rMOwXMJyUpj028cu0Gbp7qe/ihxfJMLc8VZBMMqSwLgOxDI1911gV4Enl1QSavAQNJcwmwBF9M0VvLh6Q==
dependencies:
"@octokit/types" "^13.0.0"
universal-user-agent "^7.0.2"

"@octokit/graphql@^8.0.0":
version "8.1.1"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-8.1.1.tgz#3cacab5f2e55d91c733e3bf481d3a3f8a5f639c4"
integrity sha512-ukiRmuHTi6ebQx/HFRCXKbDlOh/7xEV6QUXaE7MJEKGNAncGI/STSbOkl12qVXZrfZdpXctx5O9X1AIaebiDBg==
dependencies:
"@octokit/request" "^9.0.0"
"@octokit/types" "^13.0.0"
universal-user-agent "^7.0.0"

"@octokit/openapi-types@^22.2.0":
version "22.2.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-22.2.0.tgz#75aa7dcd440821d99def6a60b5f014207ae4968e"
integrity sha512-QBhVjcUa9W7Wwhm6DBFu6ZZ+1/t/oYxqc2tp81Pi41YNuJinbFRx8B133qVOrAaBbF7D/m0Et6f9/pZt9Rc+tg==

"@octokit/plugin-paginate-rest@^11.0.0":
version "11.3.3"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-11.3.3.tgz#efc97ba66aae6797e2807a082f99b9cfc0e05aba"
integrity sha512-o4WRoOJZlKqEEgj+i9CpcmnByvtzoUYC6I8PD2SA95M+BJ2x8h7oLcVOg9qcowWXBOdcTRsMZiwvM3EyLm9AfA==
dependencies:
"@octokit/types" "^13.5.0"

"@octokit/plugin-request-log@^5.3.1":
version "5.3.1"
resolved "https://registry.yarnpkg.com/@octokit/plugin-request-log/-/plugin-request-log-5.3.1.tgz#ccb75d9705de769b2aa82bcd105cc96eb0c00f69"
integrity sha512-n/lNeCtq+9ofhC15xzmJCNKP2BWTv8Ih2TTy+jatNCCq/gQP/V7rK3fjIfuz0pDWDALO/o/4QY4hyOF6TQQFUw==

"@octokit/plugin-rest-endpoint-methods@^13.0.0":
version "13.2.4"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-13.2.4.tgz#543add032d3fe3f5d2839bfd619cf66d85469f01"
integrity sha512-gusyAVgTrPiuXOdfqOySMDztQHv6928PQ3E4dqVGEtOvRXAKRbJR4b1zQyniIT9waqaWk/UDaoJ2dyPr7Bk7Iw==
dependencies:
"@octokit/types" "^13.5.0"

"@octokit/request-error@^6.0.1":
version "6.1.4"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-6.1.4.tgz#ad96e29148d19edc2ba8009fc2b5a24a36c90f16"
integrity sha512-VpAhIUxwhWZQImo/dWAN/NpPqqojR6PSLgLYAituLM6U+ddx9hCioFGwBr5Mi+oi5CLeJkcAs3gJ0PYYzU6wUg==
dependencies:
"@octokit/types" "^13.0.0"

"@octokit/request@^9.0.0":
version "9.1.3"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-9.1.3.tgz#42b693bc06238f43af3c037ebfd35621c6457838"
integrity sha512-V+TFhu5fdF3K58rs1pGUJIDH5RZLbZm5BI+MNF+6o/ssFNT4vWlCh/tVpF3NxGtP15HUxTTMUbsG5llAuU2CZA==
dependencies:
"@octokit/endpoint" "^10.0.0"
"@octokit/request-error" "^6.0.1"
"@octokit/types" "^13.1.0"
universal-user-agent "^7.0.2"

"@octokit/rest@^21.0.1":
version "21.0.1"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-21.0.1.tgz#b77d985ded81ac180f4efb26858311c0fcb8462c"
integrity sha512-RWA6YU4CqK0h0J6tfYlUFnH3+YgBADlxaHXaKSG+BVr2y4PTfbU2tlKuaQoQZ83qaTbi4CUxLNAmbAqR93A6mQ==
dependencies:
"@octokit/core" "^6.1.2"
"@octokit/plugin-paginate-rest" "^11.0.0"
"@octokit/plugin-request-log" "^5.3.1"
"@octokit/plugin-rest-endpoint-methods" "^13.0.0"

"@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
version "13.5.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.5.0.tgz#4796e56b7b267ebc7c921dcec262b3d5bfb18883"
integrity sha512-HdqWTf5Z3qwDVlzCrP8UJquMwunpDiMPt5er+QjGzL4hqr/vBVY/MauQgS1xWxCDT1oMx1EULyqxncdCY/NVSQ==
dependencies:
"@octokit/openapi-types" "^22.2.0"

before-after-hook@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-3.0.2.tgz#d5665a5fa8b62294a5aa0a499f933f4a1016195d"
integrity sha512-Nik3Sc0ncrMK4UUdXQmAnRtzmNQTAAXmXIopizwZ1W1t8QmfJj+zL4OA2I7XPTPW5z5TDqv4hRo/JzouDJnX3A==

libsodium-wrappers@^0.7.14:
version "0.7.14"
resolved "https://registry.yarnpkg.com/libsodium-wrappers/-/libsodium-wrappers-0.7.14.tgz#b21d9e8d58de686c6318a772805ee1c5d02035a5"
integrity sha512-300TtsePizhJZ7HjLmWr6hLHAgJUxIGhapSw+EwfCtDuWaEmEdGXSQv6j6qFw0bs9l4vS2NH9BtOHfXAq6h5kQ==
dependencies:
libsodium "^0.7.14"

libsodium@^0.7.14:
version "0.7.14"
resolved "https://registry.yarnpkg.com/libsodium/-/libsodium-0.7.14.tgz#d9daace70dbc36051b947d37999bb6337c364c88"
integrity sha512-/pOd7eO6oZrfORquRTC4284OUJFcMi8F3Vnc9xtRBT0teLfOUxWIItaBFF3odYjZ7nlJNwnLdUVEUFHxVyX/Sw==

universal-user-agent@^7.0.0, universal-user-agent@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-7.0.2.tgz#52e7d0e9b3dc4df06cc33cb2b9fd79041a54827e"
integrity sha512-0JCqzSKnStlRRQfCdowvqy3cy0Dvtlb8xecj/H8JFZuCze4rwjPZQOgvFvn0Ws/usCHQFGpyr+pB9adaGwXn4Q==

0 comments on commit fb0d526

Please sign in to comment.