Skip to content

Commit 5b2a16e

Browse files
authored
Merge pull request #26 from advanced-security/patch-codeql-loading
fix: improve token handling and logging in CodeQL extractor
2 parents 47d503e + d935f6f commit 5b2a16e

File tree

8 files changed

+41
-40
lines changed

8 files changed

+41
-40
lines changed

.release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: "codeql-extractor-action"
22
repository: "advanced-security/codeql-extractor-action"
3-
version: 0.1.3
3+
version: 0.1.4
44

55
ecosystems:
66
- Docs

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "codeql-extractor-action"
33
description = "GitHub Action for CodeQL Extractors"
4-
version = "0.1.3"
4+
version = "0.1.4"
55
authors = ["GeekMasher"]
66

77
license = "MIT"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This action is designed to be used in conjunction with the [CodeQL][CodeQL] anal
2929

3030
```yml
3131
- name: "CodeQL Extractor Action"
32-
uses: advanced-security/codeql-extractor-action@v0.1.3
32+
uses: advanced-security/codeql-extractor-action@v0.1.4
3333
with:
3434
# Repository reference (e.g. "owner/repo", "owner/repo@ref")
3535
extractor: "advanced-security/codeql-extractor-iac"

action.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM ghcr.io/advanced-security/codeql-extractor-action:v0.1.3
1+
FROM ghcr.io/advanced-security/codeql-extractor-action:v0.1.4
22

33
ENTRYPOINT [ "codeql-extractor-action" ]

src/action.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ pub struct Action {
105105
impl Action {
106106
/// Returns the GitHub Token for the action
107107
pub fn get_token(&self) -> String {
108-
if self.token.is_empty() {
109-
std::env::var("GITHUB_TOKEN").unwrap_or_default()
110-
} else {
108+
if !self.token.is_empty() {
109+
log::debug!("Using provided token");
111110
self.token.clone()
111+
} else if let Ok(gh_token) = std::env::var("GITHUB_TOKEN") {
112+
log::debug!("No token provided, using GITHUB_TOKEN environment variable");
113+
gh_token
114+
} else {
115+
log::debug!("No token provided, and GITHUB_TOKEN environment variable not set");
116+
String::new()
112117
}
113118
}
114119

@@ -205,12 +210,6 @@ impl Action {
205210
fn get_codeql_directories(&self) -> Vec<PathBuf> {
206211
let mut paths = Vec::new();
207212

208-
// GITHUB_WORKSPACE
209-
if let Ok(github_workspace) = std::env::var("GITHUB_WORKSPACE") {
210-
log::debug!("GITHUB_WORKSPACE found: {}", github_workspace);
211-
paths.push(PathBuf::from(github_workspace).join(".codeql"));
212-
}
213-
214213
// Local CodeQL directory in the working directory
215214
if let Ok(working_dir) = self.working_directory() {
216215
if let Ok(local_codeql) = working_dir.join(".codeql").canonicalize() {
@@ -219,6 +218,12 @@ impl Action {
219218
}
220219
}
221220

221+
// GITHUB_WORKSPACE
222+
if let Ok(github_workspace) = std::env::var("GITHUB_WORKSPACE") {
223+
log::debug!("GITHUB_WORKSPACE found: {}", github_workspace);
224+
paths.push(PathBuf::from(github_workspace).join(".codeql"));
225+
}
226+
222227
// Runner temp directory
223228
if let Ok(runner_temp) = std::env::var("RUNNER_TEMP") {
224229
log::debug!("RUNNER_TEMP found: {}", runner_temp);

src/codeql.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub async fn codeql_download(action: &Action) -> Result<CodeQL> {
2626

2727
// Try to install with authentication first (if token is available)
2828
if !token.is_empty() {
29-
let octocrab_auth = action.octocrab_with_token(token)?;
29+
let octocrab_auth = action.octocrab_with_token(&token)?;
3030
if let Ok(_) = codeql.install(&octocrab_auth, codeql_version).await {
3131
log::info!("CodeQL installed using authentication");
3232
return Ok(codeql);
@@ -35,6 +35,8 @@ pub async fn codeql_download(action: &Action) -> Result<CodeQL> {
3535
"Failed to install CodeQL with authentication, trying without authentication..."
3636
);
3737
}
38+
} else {
39+
log::debug!("No token provided, skipping authenticated installation attempt");
3840
}
3941

4042
// Try to install without authentication
@@ -47,15 +49,17 @@ pub async fn codeql_download(action: &Action) -> Result<CodeQL> {
4749
log::info!("Attempting to install CodeQL using GitHub CLI...");
4850
}
4951

50-
let location = gh_codeql_download(codeql_version)
51-
.await
52-
.context("Failed to download CodeQL using GitHub CLI")?;
53-
// Reinitialize CodeQL with the new path
54-
codeql = CodeQL::init()
55-
.path(location)
56-
.build()
57-
.await
58-
.context("Failed to create CodeQL instance after GitHub CLI installation")?;
52+
if !token.is_empty() {
53+
let location = gh_codeql_download(codeql_version, &token)
54+
.await
55+
.context("Failed to download CodeQL using GitHub CLI")?;
56+
// Reinitialize CodeQL with the new path
57+
codeql = CodeQL::init()
58+
.path(location)
59+
.build()
60+
.await
61+
.context("Failed to create CodeQL instance after GitHub CLI installation")?;
62+
}
5963

6064
log::info!("CodeQL installed");
6165
} else {
@@ -78,15 +82,13 @@ pub async fn codeql_download(action: &Action) -> Result<CodeQL> {
7882
///
7983
/// # Returns
8084
/// * `Result<String>` - Path to the installed CodeQL binary or an error
81-
async fn gh_codeql_download(codeql_version: &str) -> Result<String> {
85+
async fn gh_codeql_download(codeql_version: &str, token: &String) -> Result<String> {
8286
log::info!("Downloading CodeQL Extension for GitHub CLI...");
8387
log::debug!("Running command: gh extensions install github/gh-codeql");
88+
8489
let status = tokio::process::Command::new("gh")
8590
.args(&["extensions", "install", "github/gh-codeql"])
86-
.env(
87-
"GH_TOKEN",
88-
std::env::var("GITHUB_TOKEN").unwrap_or_default(),
89-
)
91+
.env("GH_TOKEN", &token)
9092
.status()
9193
.await
9294
.context("Failed to execute `gh extensions install github/gh-codeql` command")?;
@@ -107,10 +109,7 @@ async fn gh_codeql_download(codeql_version: &str) -> Result<String> {
107109
log::debug!("Running command: gh codeql set-version {codeql_version}");
108110
let status = tokio::process::Command::new("gh")
109111
.args(&["codeql", "set-version", codeql_version])
110-
.env(
111-
"GH_TOKEN",
112-
std::env::var("GITHUB_TOKEN").unwrap_or_default(),
113-
)
112+
.env("GH_TOKEN", &token)
114113
.status()
115114
.await
116115
.context("Failed to execute `gh codeql set-version` command")?;
@@ -131,10 +130,7 @@ async fn gh_codeql_download(codeql_version: &str) -> Result<String> {
131130
log::debug!("Running command: gh codeql install-stub");
132131
let status = tokio::process::Command::new("gh")
133132
.args(&["codeql", "install-stub"])
134-
.env(
135-
"GH_TOKEN",
136-
std::env::var("GITHUB_TOKEN").unwrap_or_default(),
137-
)
133+
.env("GH_TOKEN", &token)
138134
.status()
139135
.await
140136
.context("Failed to execute `gh codeql install-stub` command")?;

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ async fn main() -> Result<()> {
165165

166166
log::info!("CodeQL :: {codeql:#?}");
167167

168-
groupend!();
169-
170168
std::fs::create_dir_all(&sarif_output).context("Failed to create results directory")?;
171169

170+
groupend!();
171+
172172
for (extractor, reporef) in extractors {
173173
// The language is the name of the extractor
174174
let language = extractor.name.to_string();
@@ -248,7 +248,7 @@ async fn main() -> Result<()> {
248248
match codeql
249249
.database(&database)
250250
.queries(queries)
251-
.output(sarif_path.clone())
251+
.sarif(sarif_path.clone())
252252
.analyze()
253253
.await
254254
{

0 commit comments

Comments
 (0)