Skip to content

Commit

Permalink
fix: debug wip
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Jan 7, 2025
1 parent 3def986 commit 46ab261
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
14 changes: 14 additions & 0 deletions packages/action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,55 @@ async function run() {
const tokenBureauUrl = core.getInput('token-bureau-url', { required: true });
const audience = core.getInput('audience', { required: true });

core.debug(`Using token-bureau-url: ${tokenBureauUrl}`);
core.debug(`Using audience: ${audience}`);

// Get OIDC token from GitHub Actions
const idToken = await core.getIDToken(audience);
core.debug('Successfully obtained OIDC token');

// Extract current repository from environment
const repository = process.env.GITHUB_REPOSITORY?.split('/')[1];
if (!repository) {
throw new Error('GITHUB_REPOSITORY environment variable is not set');
}
core.debug(`Repository: ${repository}`);

// Request token from TokenBureau
core.debug('Sending request to TokenBureau');
const response = await fetch(`${tokenBureauUrl}/generate-token`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${idToken}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'token-bureau-action'
},
body: JSON.stringify({
repositories: [repository]
})
});

core.debug(`Response status: ${response.status}`);

if (!response.ok) {
const error = await response.text();
core.error(`Error response: ${error}`);
throw new Error(`Failed to get token: ${error}`);
}

const data = await response.json();
core.debug('Successfully received token response');

// Set outputs
core.setSecret(data.token);
core.setOutput('token', data.token);
core.setOutput('expires_at', data.expires_at);
core.setOutput('installation_id', data.installation_id);

core.debug('Action completed successfully');
} catch (error) {
core.error(`Action failed: ${error.message}`);
core.setFailed(error.message);
}
}
Expand Down
32 changes: 17 additions & 15 deletions packages/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ app.use((req, res, next) => {
requestId,
method: req.method,
url: req.url,
ip: req.ip
ip: req.ip,
headers: req.headers, // Add headers logging
}, 'Incoming request');

// Log response
Expand Down Expand Up @@ -185,31 +186,29 @@ async function generateToken(owner, repository) {
}

function extractAndDecodeToken(authHeader) {
logger.debug({ authHeader }, 'Processing authorization header'); // Add debug log

if (!authHeader?.startsWith('Bearer ')) {
logger.error({ authHeader }, 'Authorization header missing or invalid format');
throw new Error('Missing or invalid Authorization header');
}

let tokenPayload = authHeader.split(' ')[1];

logger.debug('Token payload received');

// Try to parse as JSON first
try {
const parsed = JSON.parse(tokenPayload);
if (parsed.value) {
logger.debug('Found token in JSON value field');
tokenPayload = parsed.value;
}
} catch (e) {
logger.debug('Token is not in JSON format, using as is');
}
logger.debug({ tokenPayload: tokenPayload.substring(0, 20) + '...' }, 'Token payload received');

// Remove JSON parsing attempt as it's not needed
// The token should always be a plain JWT string

// Remove any whitespace or quotes
tokenPayload = tokenPayload.trim().replace(/^["']|["']$/g, '');

logger.debug({ tokenLength: tokenPayload.length }, 'Processed token length');

// Basic JWT structure validation
const parts = tokenPayload.split('.');
if (parts.length !== 3) {
logger.error({ parts: parts.length }, 'Invalid JWT structure');
throw new Error('Invalid JWT format - token must have three parts');
}

Expand All @@ -219,7 +218,10 @@ function extractAndDecodeToken(authHeader) {
// Route to generate GitHub App token
app.post('/generate-token', async (req, res) => {
try {
logger.debug('Processing token generation request');
logger.debug({
headers: req.headers,
body: req.body
}, 'Processing token generation request');

const tokenPayload = extractAndDecodeToken(req.headers.authorization);

Expand All @@ -238,7 +240,7 @@ app.post('/generate-token', async (req, res) => {
});
}

logger.debug('Token verified successfully');
logger.debug({ decoded }, 'Token verified successfully');

// Extract repository information from the token
const repo = decoded.repository;
Expand Down

0 comments on commit 46ab261

Please sign in to comment.