forked from DependencyTrack/dependency-track
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Walter de Boer <[email protected]>
- Loading branch information
Walter de Boer
committed
Mar 11, 2023
1 parent
3a5989a
commit d324a67
Showing
13 changed files
with
274 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ target/ | |
.idea/ | ||
.vscode/ | ||
|
||
# windows | ||
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/org/dependencytrack/tasks/repositories/CpanMetaAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.tasks.repositories; | ||
|
||
import java.io.IOException; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.util.EntityUtils; | ||
import org.dependencytrack.model.Component; | ||
import org.dependencytrack.model.RepositoryType; | ||
import org.json.JSONObject; | ||
import alpine.common.logging.Logger; | ||
|
||
/** | ||
* An IMetaAnalyzer implementation that supports CPAN. | ||
*/ | ||
public class CpanMetaAnalyzer extends AbstractMetaAnalyzer { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CpanMetaAnalyzer.class); | ||
private static final String DEFAULT_BASE_URL = "https://fastapi.metacpan.org/v1"; | ||
private static final String API_URL = "/module/%s"; | ||
|
||
CpanMetaAnalyzer() { | ||
this.baseUrl = DEFAULT_BASE_URL; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public boolean isApplicable(final Component component) { | ||
return component.getPurl() != null && "cpan".equals(component.getPurl().getType()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public RepositoryType supportedRepositoryType() { | ||
return RepositoryType.CPAN; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public MetaModel analyze(final Component component) { | ||
final MetaModel meta = new MetaModel(component); | ||
if (component.getPurl() != null) { | ||
|
||
final String packageName; | ||
if (component.getPurl().getNamespace() != null) { | ||
packageName = component.getPurl().getNamespace() + "%2F" + component.getPurl().getName(); | ||
} else { | ||
packageName = component.getPurl().getName(); | ||
} | ||
|
||
final String url = String.format(baseUrl + API_URL, packageName); | ||
try (final CloseableHttpResponse response = processHttpRequest(url)) { | ||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
if (response.getEntity()!=null) { | ||
String responseString = EntityUtils.toString(response.getEntity()); | ||
var jsonObject = new JSONObject(responseString); | ||
final String latest = jsonObject.optString("version"); | ||
if (latest != null) { | ||
meta.setLatestVersion(latest); | ||
} | ||
final String published = jsonObject.optString("date"); | ||
if (published != null) { | ||
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
try { | ||
meta.setPublishedTimestamp(dateFormat.parse(published)); | ||
} catch (ParseException e) { | ||
LOGGER.warn("An error occurred while parsing upload time", e); | ||
} | ||
} | ||
} | ||
} else { | ||
handleUnexpectedHttpResponse(LOGGER, url, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), component); | ||
} | ||
} catch (IOException e) { | ||
handleRequestException(LOGGER, e); | ||
} | ||
} | ||
return meta; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.