|
1 | 1 | package no.digipost.github.monitoring
|
2 | 2 |
|
3 |
| -import com.apollographql.apollo3.ApolloCall |
4 | 3 | import com.apollographql.apollo3.ApolloClient
|
5 |
| -import com.apollographql.apollo3.api.ApolloResponse |
6 | 4 | import com.apollographql.apollo3.api.Optional
|
7 | 5 | import com.apollographql.apollo3.exception.ApolloHttpException
|
8 | 6 | import com.github.graphql.client.GetVulnerabilityAlertsForRepoQuery
|
9 | 7 | import com.github.graphql.client.QueryRepositoriesQuery
|
| 8 | +import java.io.IOException |
10 | 9 | import kotlinx.coroutines.channels.Channel
|
11 |
| -import kotlinx.coroutines.flow.Flow |
12 | 10 | import kotlinx.coroutines.flow.catch
|
13 |
| -import kotlinx.coroutines.flow.filterNotNull |
14 | 11 | import kotlinx.coroutines.flow.first
|
15 |
| -import kotlinx.coroutines.flow.map |
| 12 | +import kotlinx.coroutines.flow.flow |
16 | 13 | import kotlinx.coroutines.launch
|
17 | 14 | import kotlinx.coroutines.runBlocking
|
18 | 15 | import okhttp3.internal.immutableListOf
|
19 | 16 | import okhttp3.internal.toImmutableList
|
20 | 17 | import org.slf4j.Logger
|
21 | 18 | import org.slf4j.LoggerFactory
|
22 |
| -import java.io.IOException |
23 | 19 |
|
24 | 20 | data class Repos(val all: List<Repository>) {
|
25 | 21 | fun getUniqueCVEs(): Map<String, Vulnerability> {
|
@@ -113,29 +109,40 @@ private suspend fun getVulnerabilitiesForRepo(
|
113 | 109 |
|
114 | 110 | while (hasNext) {
|
115 | 111 |
|
116 |
| - val response = apolloClient.query(GetVulnerabilityAlertsForRepoQuery(name, GITHUB_OWNER, after = Optional.present(cursor))).toFlow() |
117 |
| - .catch { ex -> logger.error("Noe gikk galt i henting av sårbarheter fra Github", ex) } |
118 |
| - .first() |
| 112 | + try { |
| 113 | + val response = apolloClient |
| 114 | + .query(GetVulnerabilityAlertsForRepoQuery(name, GITHUB_OWNER, after = Optional.present(cursor))) |
| 115 | + .toFlow().first(); |
119 | 116 |
|
120 |
| - val vulnerabilityAlerts = response.data?.repository?.vulnerabilityAlerts?.nodes ?: emptyList() |
121 |
| - val vulnerabilities = vulnerabilityAlerts.mapNotNull { |
122 |
| - it?.let { |
123 |
| - Vulnerability( |
124 |
| - it.securityVulnerability!!.severity, |
125 |
| - it.createdAt.toString().substring(0, 10), |
126 |
| - it.securityVulnerability.`package`.name, |
127 |
| - it.securityVulnerability.advisory.cvss.score, |
128 |
| - it.securityVulnerability.advisory.identifiers.firstOrNull { identifier -> "CVE" == identifier.type }?.value |
129 |
| - ?: "ukjent CVE" |
130 |
| - ) |
131 |
| - } |
132 |
| - }.toImmutableList() |
| 117 | + val vulnerabilityAlerts = response.data?.repository?.vulnerabilityAlerts?.nodes ?: emptyList() |
| 118 | + val vulnerabilities = vulnerabilityAlerts.mapNotNull { |
| 119 | + it?.let { |
| 120 | + Vulnerability( |
| 121 | + it.securityVulnerability!!.severity, |
| 122 | + it.createdAt.toString().substring(0, 10), |
| 123 | + it.securityVulnerability.`package`.name, |
| 124 | + it.securityVulnerability.advisory.cvss.score, |
| 125 | + it.securityVulnerability.advisory.identifiers.firstOrNull { identifier -> "CVE" == identifier.type }?.value |
| 126 | + ?: "ukjent CVE" |
| 127 | + ) |
| 128 | + } |
| 129 | + }.toImmutableList() |
| 130 | + |
| 131 | + allVulnerabilities = allVulnerabilities + vulnerabilities |
133 | 132 |
|
134 |
| - allVulnerabilities = allVulnerabilities + vulnerabilities |
| 133 | + hasNext = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.hasNextPage ?: false |
135 | 134 |
|
136 |
| - hasNext = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.hasNextPage ?: false |
| 135 | + cursor = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.endCursor |
| 136 | + } catch (e: Exception) { |
| 137 | + if (e is ApolloHttpException && e.statusCode == 504) { |
| 138 | + logger.warn("Fikk 504 fra ApolloGraphQL for {}, skipper", name) |
| 139 | + continue |
| 140 | + } else { |
| 141 | + logger.error("Noe gikk galt i henting av sårbarheter fra Github", e) |
| 142 | + throw e |
| 143 | + } |
| 144 | + } |
137 | 145 |
|
138 |
| - cursor = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.endCursor |
139 | 146 | }
|
140 | 147 |
|
141 | 148 | return allVulnerabilities
|
|
0 commit comments