Fix EISDIR error when credentials.json is an empty directory due to missing k8s secret

This commit is contained in:
Antigravity 2026-06-13 22:53:04 -04:00
parent 3cc80aeba5
commit dcb6f952cc
1 changed files with 15 additions and 7 deletions

View File

@ -7,6 +7,13 @@ const { google } = require('googleapis');
const TOKENS_DIR = path.join(__dirname, 'tokens'); const TOKENS_DIR = path.join(__dirname, 'tokens');
const CREDENTIALS_PATH = path.join(__dirname, 'credentials.json'); const CREDENTIALS_PATH = path.join(__dirname, 'credentials.json');
function getCredentials() {
if (!fs.existsSync(CREDENTIALS_PATH) || fs.statSync(CREDENTIALS_PATH).isDirectory()) {
throw new Error('Google OAuth credentials.json is missing. Please create the gmail-agent-auth Kubernetes Secret with your Google Cloud OAuth credentials.');
}
return JSON.parse(fs.readFileSync(CREDENTIALS_PATH));
}
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://ollama.ai-agents.svc.cluster.local:11434/api/generate'; const OLLAMA_URL = process.env.OLLAMA_URL || 'http://ollama.ai-agents.svc.cluster.local:11434/api/generate';
const WHATSAPP_GATEWAY_URL = process.env.WHATSAPP_GATEWAY_URL || 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/api/send-message'; const WHATSAPP_GATEWAY_URL = process.env.WHATSAPP_GATEWAY_URL || 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/api/send-message';
const WHATSAPP_TO = process.env.WHATSAPP_TO || '+14085505485'; const WHATSAPP_TO = process.env.WHATSAPP_TO || '+14085505485';
@ -16,7 +23,8 @@ let baseOAuth2Client;
let accounts = []; let accounts = [];
function loadAuth() { function loadAuth() {
if (!fs.existsSync(CREDENTIALS_PATH)) { try {
if (!fs.existsSync(CREDENTIALS_PATH) || fs.statSync(CREDENTIALS_PATH).isDirectory()) {
console.warn('⚠️ Missing credentials.json. Gmail Agent will not be able to process emails or authenticate new accounts until this file is provided.'); console.warn('⚠️ Missing credentials.json. Gmail Agent will not be able to process emails or authenticate new accounts until this file is provided.');
return; return;
} }
@ -26,7 +34,7 @@ function loadAuth() {
} }
try { try {
const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); const creds = getCredentials();
const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; const {client_secret, client_id, redirect_uris} = creds.installed || creds.web;
baseOAuth2Client = { client_id, client_secret, redirect_uri: redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob' }; baseOAuth2Client = { client_id, client_secret, redirect_uri: redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob' };
@ -204,7 +212,7 @@ app.post('/api/auth/url', (req, res) => {
if (!accountName) return res.status(400).json({ success: false, error: 'accountName is required' }); if (!accountName) return res.status(400).json({ success: false, error: 'accountName is required' });
try { try {
const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); const creds = getCredentials();
const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; const {client_secret, client_id, redirect_uris} = creds.installed || creds.web;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob'); const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob');
@ -226,7 +234,7 @@ app.post('/api/auth/token', (req, res) => {
if (!accountName || !code) return res.status(400).json({ success: false, error: 'accountName and code are required' }); if (!accountName || !code) return res.status(400).json({ success: false, error: 'accountName and code are required' });
try { try {
const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); const creds = getCredentials();
const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; const {client_secret, client_id, redirect_uris} = creds.installed || creds.web;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob'); const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob');