Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect and parse as-installed Alpine Linux packages #2061

Closed
pombredanne opened this issue Jun 4, 2020 · 5 comments
Closed

Collect and parse as-installed Alpine Linux packages #2061

pombredanne opened this issue Jun 4, 2020 · 5 comments
Assignees

Comments

@pombredanne
Copy link
Contributor

As part of #2058 we would need to collect as-installed Alpine packages (and that is in addition to support .apk files in #1803 )
We already have a base for that since @chinyeungli shared this with me:

 #
#  Copyright (c) 2019 nexB Inc. and others. All rights reserved.
#

import click
import csv
import posixpath

# This utility will only parse the P: (Package), V: (Version), U: (URL) and L: (License)
# from the installed text file
def parse_alpine_apk(input, output):
    with open(input) as f:
        content = f.readlines()
        installed_list = []
        package_data = []
        for x in content:
            # Remove white space or newline char
            x = x.strip()
            if x.startswith('P:'):
                if package_data:
                    installed_list.append(package_data)
                package_data = []
                data = x.partition(':')[2]
                package_data.append(data)
            if x.startswith('V:'):
                data = x.partition(':')[2]
                package_data.append(data)
            if x.startswith('U:'):
                data = x.partition(':')[2]
                package_data.append(data)
            if x.startswith('L:'):
                data = x.partition(':')[2]
                package_data.append(data)
        # Append the last package to the installed_list
        if package_data:
                    installed_list.append(package_data)

    with open(output, 'wb') as csvfile:
        cwriter = csv.writer(csvfile, delimiter=',')
        header_row = ['Package', 'Version', 'URL', 'License']
        cwriter.writerow(header_row)
        for r in installed_list:
            cwriter.writerow(r)

    print("FINISHED!!")

@click.command()
@click.argument('input', type=click.Path(exists=True, readable=True))
@click.argument('output', type=click.Path(exists=False), required=True)
def cli(input, output):
    parse_alpine_apk(input, output)

with this documentation by @johnmhoran :

================
Parse Alpine APK
================

Usage
=====

.. code-block:: none

   Usage: parse_alpine_apk [OPTIONS] LOCATION DESTINATION

     Command Line API to parse the apline_apk installed file and store to a CSV file.

   Options:
     --help  Show this message and exit.

Example
=======

.. code-block:: none

   parse_alpine_apk /code/project/layer.tar-extract/lib/apk/db/installed /audit_workspace/scans/installed_packages.csv

Notes
=====

This installed file is usually located at ``/lib/apk/db/installed`` under the ``layer.tar``. This file shows what packages (including dependencies) are installed.

This utility will only parse the P: (Package), V: (Version), U: (URL) and L: (License) from the installed text file.

The format of a installed file is a set of KEY=. A sample file would look like:

.. code-block:: none

   C:Q1t20ETDkiZIy7BdegtC/woQiNnuE=
   P:musl
   V:1.1.19-r10
   A:x86_64
   S:372275
   I:602112
   T:the musl c library (libc) implementation
   U:http://www.musl-libc.org/
   L:MIT
   o:musl
   m:Timo Teräs <timo.teras@iki.fi>
   t:1529394495
   c:ed42835662421a72dbc1c47397a2805306203860
   p:so:libc.musl-x86_64.so.1=1
   F:lib
   R:libc.musl-x86_64.so.1
   a:0:0:777
   Z:Q17yJ3JFNypA4mxhJJr0ou6CzsJVI=
   R:ld-musl-x86_64.so.1
   a:0:0:755
   Z:Q1lUgQ+IQG9688iKiZSRz3ITg7h/Y=
   F:usr
   F:usr/lib

Other useful sources include:

@steven-esser steven-esser self-assigned this Jun 5, 2020
@awilfox
Copy link

awilfox commented Jun 28, 2020

apkkit is considered, for all intents and purposes, to be "complete". That is, if any bugs are found, we will fix them - but no new features are planned at this time.

@pombredanne
Copy link
Contributor Author

apkkit is considered, for all intents and purposes, to be "complete". That is, if any bugs are found, we will fix them - but no new features are planned at this time.

@awilfox thanks you ++

@pombredanne
Copy link
Contributor Author

@JonoYang you can also check this aboutcode-org/debian-inspector#3 where the debian parser (which is essentially the built stdlib email header parser actually works quite well)

@pombredanne
Copy link
Contributor Author

@JonoYang the code actually handling Alpine could be quite similar to what in WIP for Debian in this PR https://github.com/nexB/scancode-toolkit/tree/2058-collect-system-packages and could likely be added on top

JonoYang added a commit that referenced this issue Jul 7, 2020
Signed-off-by: Jono Yang <jyang@nexb.com>
JonoYang added a commit that referenced this issue Jul 7, 2020
Signed-off-by: Jono Yang <jyang@nexb.com>
@pombredanne
Copy link
Contributor Author

This has been completed and is available in the latest release ... follow up tickets are tracked separately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants