-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck_secrets.js
117 lines (99 loc) · 3.68 KB
/
check_secrets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require('dotenv').config();
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
// Function to test if a string looks like a PAT
function looksLikePAT(str) {
if (!str) return false;
// PATs are typically long strings
if (str.length < 32) return false;
// Check if it's already prefixed with "Key"
if (str.startsWith('Key ')) {
str = str.substring(4);
}
// Basic format check - should be alphanumeric
return /^[a-zA-Z0-9_-]+$/.test(str);
}
// Function to test PAT authentication
async function testPATAuth(pat) {
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
const authValue = pat.startsWith('Key ') ? pat : `Key ${pat}`;
metadata.set('authorization', authValue);
return new Promise((resolve, reject) => {
stub.ListApps(
{
page: 1,
per_page: 1
},
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
}
async function main() {
const secrets = {
'clarifai_api': process.env.clarifai_api,
'new_clarifai_key': process.env.new_clarifai_key
};
console.log('Examining Clarifai-related secrets...\n');
for (const [name, value] of Object.entries(secrets)) {
console.log(`\nChecking ${name}:`);
if (!value) {
console.log(' ✗ Not set');
continue;
}
console.log(` Value: ${value.substring(0, 4)}...${value.substring(value.length - 4)}`);
console.log(` Length: ${value.length} characters`);
if (!looksLikePAT(value)) {
console.log(' ✗ Does not match PAT format');
continue;
}
console.log(' ✓ Matches PAT format');
console.log(' Testing authentication...');
try {
const response = await testPATAuth(value);
if (response.apps && response.apps.length > 0) {
const app = response.apps[0];
console.log(' ✓ Authentication successful!');
console.log(` User ID: ${app.user_id}`);
console.log(` App ID: ${app.id}`);
// Update .env file with working credentials
const fs = require('fs');
const envPath = '.env';
let envContent = '';
try {
envContent = fs.readFileSync(envPath, 'utf8');
} catch (err) {
envContent = '';
}
const updates = {
'CLARIFAI_PAT': value,
'USER_ID': app.user_id,
'APP_ID': app.id
};
for (const [envKey, envValue] of Object.entries(updates)) {
const regex = new RegExp(`^${envKey}=.*$`, 'm');
const newLine = `${envKey}=${envValue}`;
if (regex.test(envContent)) {
envContent = envContent.replace(regex, newLine);
} else {
envContent += envContent.endsWith('\n') ? newLine + '\n' : '\n' + newLine + '\n';
}
}
fs.writeFileSync(envPath, envContent);
console.log('\nEnvironment variables updated successfully!');
process.exit(0);
}
} catch (error) {
console.log(` ✗ Authentication failed: ${error.message}`);
}
}
console.log('\nNo valid PATs found among available secrets.');
process.exit(1);
}
main().catch(console.error);