How do I retrieve an artifact checksum from Nexus using their rest api via curl? - rest

I am trying to verify the checksum of the artifacts I am downloading from Nexus. I can grab the artifact and download them and check their md5sum or sha1sum, but I need to check this against the actual sum from Nexus so I can verify they are correct.
This is the command I'm using to grab files from Nexus:
curl -v -L -o /mylocation/artifact.war -u 'myuser:mypass' --get 'http://ournexus.com/service/local/artifact/maven/content?g=com.ours.stuff&a=our-service-war&v=LATEST&r=snapshots&p=war'
Via http://nexus.xwiki.org/nexus/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html, it would appear that I can also search for the sha1 sum, but when I do &sha1 I get nothing extra or sha1=(sum), nothing is pulled up, even if I omit all the above options.
This works, but it goes to a specific war, and we need the latest (obviously):
http://ournexus.com/service/local/repositories/snapshots/content/com/ours/stuff/ourapp/1.0.0-SNAPSHOT/ourapp-1.0.0-20140730.173704-88.war.sha1
Is this possible, am I on the right track?

You can either fetch the file directly or use the Nexus API to retrieve it programmatically.
The following URL:
http://localhost:8081/nexus/service/local/artifact/maven/resolve?g=log4j&a=log4j&v=1.2.9&r=central
Returns the following result:
<artifact-resolution>
<data>
<presentLocally>true</presentLocally>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
<extension>jar</extension>
<snapshot>false</snapshot>
<snapshotBuildNumber>0</snapshotBuildNumber>
<snapshotTimeStamp>0</snapshotTimeStamp>
<sha1>55856d711ab8b88f8c7b04fd85ff1643ffbfde7c</sha1>
<repositoryPath>/log4j/log4j/1.2.9/log4j-1.2.9.jar</repositoryPath>
</data>
</artifact-resolution>
The xmllint command can be used to parse out the sha1 checksum value as follows:
$ curl -s "http://localhost:8081/nexus/service/local/artifact/maven/resolve?g=log4j&a=log4j&v=1.2.9&r=central" | xmllint --xpath "///sha1/text()" -
55856d711ab8b88f8c7b04fd85ff1643ffbfde7c

Nexus 2
Use the artifact content API to directly get the MD5/SHA1 checksum file by specifying the p (packaging) or e (extension) parameter as jar.md5 or jar.sha1 (or other relevant for your actual packaging).
Example:
$ curl -s 'http://mynexus/service/local/artifact/maven/content?g=com.example&a=example&v=1.2.3&r=my-repo&e=jar.sha1'
55856d711ab8b88f8c7b04fd85ff1643ffbfde7c
Nexus 3
Use the Search API to find and download the asset.
$ curl -sL 'https://mynexus/service/rest/v1/search/assets/download?repository=my-repo&sort=version&maven.groupId=com.example&maven.artifactId=example&maven.baseVersion=LATEST-SNAPSHOT&maven.extension=jar.sha1&maven.classifier='
7ff1ca9fb889c73d095b69a52d5c8609482b63ab

The query below works for me
curl -u USER:PASS -X GET 'https://nexus.example.com:8443/service/rest/v1/search?repository=REPO_NAME&name=FILE_NAME' | jq '.items[0].assets[0].checksum'
Always good to check the API doc.
ps: username and password might be not needed for GET

Related

how to use filter parameters and Grep command together for listing all GITHUB pull request from a repository

I am looking for an option to get all pull requests with ID, Name, Date, and count of files committed from GitHub.
I managed to get all pull requests using API, but listing only the required parameter is not working. Can anyone help?
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all&page=1&per_page=100| egrep -w 'title|number|state|created_at|updated_at'
Grep is working if the query is like below, without multiple filter parameters (state=all&page=1&per_page=100)
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all| egrep -w 'title|number|state|created_at|updated_at'
Can anyone help how to list only the required data with multipe filter parameter.
try grep with something like this:
grep -Pa 'state'

Not able to download artifact from Nexus using curl

I am trying to download an artifact from Nexus Pro 3.37.3-02 using curl but it is not working. I have found below commands which I think working for many other but in my case I am getting repository, maven.groupId, maven.artifactId, maven.baseVersion, maven.extension is not recognized as internal or external command, operable program or batch files.
curl -u usename:password -L -X GET https://MY_NEXUS/service/rest/v1/search/assets/download?sort=version&repository=MY-REPO&maven.groupId=MY_GROUP&maven.artifactId=MY_ARTIFACT_NAME&maven.baseVersion=0.1-SNAPSHOT&maven.extension=zip
I have followed this documentation to make this command.
https://help.sonatype.com/repomanager3/integrations/rest-and-integration-api/search-api
Any help will be appreciated.
Thanks,
In case if anyone else facing the same problem then this is the solution which worked for me.
curl -u token:tokenPassword -L -X GET "https://MY_NEXUS/service/rest/v1/search/assets/download?sort=version&repository=MY-REPO&group=MY_GROUP&name=MY_ARTIFACT_NAME&version=MY_Version&maven.extension=zip" --output My_Artifact.zip

Retrieving Details from GitHub Repos

I'm looking for a way to get both the version number and license details from a repo providing I have the URL for the Repo. I have a way at the moment that doesn't work for all repos I am reviewing but it's basically html scraping.
I assume there is an API example somewhere that pulls these details?
some random examples
https://github.com/Microsoft/Terminal
https://github.com/leoasis/redux-immutable-state-invariant
https://github.com/zeroclipboard/zeroclipboard
What version do you need ? If a package.json file is present, you can use it and get the version in it:
curl -sL https://raw.githubusercontent.com/leoasis/redux-immutable-state-invariant/master/package.json | jq -r '.version'
For the latest release tag name (aka version), use
curl -sL https://api.github.com/repos/Microsoft/Terminal/releases/latest | jq -r '.tag_name'
To retreive the license, use the Github API and go to https://api.github.com/repos/zeroclipboard/zeroclipboard/license, e.g.
curl -sL https://api.github.com/repos/zeroclipboard/zeroclipboard/license | jq -r '.license.name'
`

Filtering on labels in Docker API not working (possible bug?)

I'm using the Docker API to get info on containers in JSON format. Basically, I want to do a filter based on label values, but it is not working (just returns all containers). This filter query DOES work if you just use the command line docker, i.e.:
docker ps -a -f label=owner=fred -f label=speccont=true
However, if I try to do the equivalent filter query using the API, it just returns ALL containers (no filtering done), i.e.:
curl -s --unix-socket /var/run/docker.sock http:/containers/json?all=true&filters={"label":["speccont=true","owner=fred"]}
Note that I do uri escape the filters param when I execute it, but am just showing it here unescaped for readability.
Am I doing something wrong here? Or does this seem to be a bug in the Docker API? Thanks for any help you can give!
The correct syntax for filtering containers by label as of Docker API v1.41 is
curl -s -G -X GET --unix-socket /var/run/docker.sock http://localhost/containers/json" \
--data 'all=true' \
--data-urlencode 'filters={"label":["speccont=true","owner=fred"]}'
Note the automatic URL encoding as mentioned in this stackexchange post.
I felt there was a bug with API too. But turns out there is none. I am on API version 1.30.
I get desired results with this call:
curl -sS localhost:4243/containers/json?filters=%7B%22ancestor%22%3A%20%5B%222bab985010c3%22%5D%7D
I got the url escaped string using used above with:
python -c 'import urllib; print urllib.quote("""{"ancestor": ["2bab985010c3"]}""")'

How do I deploy a file to Artifactory using the command line?

I've spent far more time on this than I care to admit. I am trying to just deploy one file into my Artifactory server from the command line. I'm doing this using gradle because that is how we manage our java builds. However, this artifact is an NDK/JNI build artifact, and does not use gradle.
So I just need the simplest gradle script to do the deploy. Something equivalent to:
scp <file> <remote>
I am currently trying to use the artifactory plugin, and am having little luck in locating a reference for the plugin.
curl POST did not work for me . PUT worked correctly . The usage is
curl -X PUT $SERVER/$PATH/$FILE --data-binary #localfile
example :
$ curl -v --user username:password --data-binary #local-file -X PUT "http://<artifactory server >/artifactory/abc-snapshot-local/remotepath/remotefile"
Instead of using the curl command, I recommend using the jfrog CLI.
Download from here - https://www.jfrog.com/getcli/ and use the following command (make sure the file is executable) -
./jfrog rt u <file-name> <upload-path>
Here is a simple example:
./jfrog rt u sample-service-1.0.0.jar libs-release-local/com/sample-service/1.0.0/
You will be prompted for credentials and the repo URL the first time.
You can do lots of other stuff with this CLI tool. Check out the detailed instructions here - https://www.jfrog.com/confluence/display/RTF/JFrog+CLI.
The documentation for the artifactory plugin can be found, as expected, in Artifactory User Guide.
Please note that it is adviced to use the newer plugin - artifactory-publish, which supports the new Gradle publishing model.
Regarding uploading from the command line, you really don't need gradle for that. You can execute a simple PUT query using CURL or any other tool.
And of course if you just want to get your file into Artifactory, you can always deploy it via the UI.
Take a look the Artifactory REST API, mostly you can't use scp command, instead use the curl command towards REST API.
$ curl -X POST $SERVER/$PATH/$FILE --data #localfile
Mostly it looks like
$ curl -X POST http://localhost:8081/artifactory/abc-snapshot-local/remotepath/remotefile --data #localfile
The scp command is only used if you really want to access the internal folder which is managed by artifactory
$ curl -v -X PUT \
--user username:password \
--upload-file <path to your file> \
http://localhost:8080/artifactory/libs-release-local/my/jar/1.0/jar-1.0.jar
Ironically, I'm answering my own question. After a couple more hours working on the problem, I found a sample project on github: https://github.com/JFrogDev/project-examples
The project even includes a straightforward bash script for doing the exact deploy/copy from the command line that I was looking for, as well as a couple of less straightforward gradle scripts.
As per official docs, You can upload any file using the following command:
curl -u username:password -T <PATH_TO_FILE> "https://<ARTIFACTORY_SERVER>/<REPOSITORY_PATH>/<TARGET_FILE>"
Note: The user should have write access to this path.