Skip to content

Commit 7e27b1c

Browse files
author
Janez Justin
committed
Code polish and readme update
1 parent 0aa92a9 commit 7e27b1c

5 files changed

Lines changed: 124 additions & 47 deletions

File tree

Makefile_example

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
NAME := pkgname
2-
URL := https://InsertUrl.here
3-
DESC := "description"
4-
MAINTAINER := "Maintainer name"
5-
LICENSE := "License here"
6-
DEPENDENCIES := "" #Add dependencies here and add '-d $(DEPENDENCIES)' to fpm in pkg/% block
1+
NAME = pkgname
2+
URL = https://InsertUrl.here
3+
DESC = "description"
4+
MAINTAINER = "Maintainer name"
5+
LICENSE = "License here"
6+
DEPENDENCIES = "" #Add dependencies here and add '-d $(DEPENDENCIES)' to fpm in pkg/% block
77

88
PKGDIR := ./pkg
99
VERSION ?= $(shell cat ./VERSION)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ Both folders contain more detailed info on setting up S3 bucket and lambda funct
1212

1313
It is possible to automate deployment of packages by combining this repository with Travis CI.
1414

15-
Examples of `.travis.yml` and `Makefile` used for autamatic deployment of go project can be found in repository
15+
Examples of `.travis.yml` and `Makefile` used for automatic deployment of go project can be found in repository

deb/Makefile

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
ZIPPED := s3apt.py gnupg.py debian/*
1+
ZIPPED_FILES := s3apt.py requirements/gnupg.py # files to compress in root of zip
2+
ZIPPED_DIR := debian # folders to compress to root of zip
23

4+
all: requires package
35

4-
set: requires package
6+
help: ## displays this message
7+
@grep -E '^[a-zA-Z_/%\-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
58

6-
requires:
7-
pip install -t . -r requirements.txt
8-
9-
package:
10-
zip code.zip $(ZIPPED)
9+
requires: ## installs required packages
10+
pip install -t ./requirements -r requirements.txt
11+
12+
package: ## creates zip of code
13+
zip -j code.zip $(ZIPPED_FILES)
14+
cd requirements && zip -r ../code.zip $(ZIPPED_DIR)
15+
16+
clean: ## cleans up the repository
17+
/bin/rm -rf code.zip
18+
/bin/rm -rf ./requirements

deb/README.md

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
21
# AWS Lambda APT repository manager for S3
32

43
Rewrite of [szinck/s3apt](https://github.com/szinck/s3apt) with a few changes and extra features - Release file is being generated and is signed with GPG key provided
54

6-
## Setting up S3 and Lambda
5+
## Readme contents
6+
7+
* [Setting up code, S3 and Lambda](#setting-up-code-s3-and-lambda)
8+
* [Getting the code](#getting-the-code)
9+
* [GPG key](#gpg-key)
10+
* [Environmental variables](#environmental-variables)
11+
* [Set up role](#set-up-role)
12+
* [Set up lambda with CLI](#set-up-lambda-with-cli)
13+
* [Set up lambda manually](#set-up-lambda-manually)
14+
* [The triggers](#the-triggers)
15+
* [Set up S3](#set-up-s3)
16+
* [Setting up apt](#setting-up-apt)
17+
* [Notes](#notes)
18+
19+
## Setting up code, S3 and Lambda
720

8-
Clone the repo and get all other required files
21+
### Getting the code
22+
Clone the repo, get all other required files and compress them
923
```
1024
git clone https://github.com/tactycal/lambdaRepos.git
1125
cd lambdaRepos/deb
12-
pip install -t . -r requirements.txt
26+
make all
1327
```
1428

15-
Compress all needed files
29+
### GPG key
30+
create your gpg key (skip to exporting your key, if you already have it)
1631
```
17-
zip code.zip s3apt.py gnupg.py debian/*
32+
gpg --gen-key
33+
# Follow the instructions
34+
# Create 'RSA and RSA' key - option 1
35+
# For maxium encryption it is recommended to make 4096 bits long key
36+
# Key should not expire
1837
```
19-
Or just use `make set` instead of `zip` and `pip` command
2038

21-
Presuming you already have GPG key generated export secret key (you can skip this part if you don't want to GPG sign your repository)
39+
export your key
40+
2241
```
23-
gpg -a --export-secret-key > secret.key
42+
gpg --export-secret-key -a "User Name" > secret.key # exports secret key to secret.key
2443
```
2544

26-
Create new lambda function, set handler to **s3apt.lambda_handler**, runtime to **python 2.7** and triggers to:
27-
28-
* Object Created(All), suffix 'deb'
29-
* Object Removed(All), suffix 'deb'
30-
* If you are using certain directory as a repo, set it as prefix
31-
32-
Upload `code.zip` to lambda function
33-
34-
Set the environmental variables
35-
45+
### Environmental variables
3646
| Key | Value |
3747
| --- | ---|
3848
| PUBLIC | True/False |
@@ -51,10 +61,59 @@ Set the environmental variables
5161

5262
**CACHE_PREFIX** Path to folder for packages cache(e.g. deb/cache)
5363

64+
### Set up role
65+
66+
Create new role with s3 write/read access
67+
68+
Here is a minimal requirement for the policy that is included in role:
69+
```
70+
{"Version": "2012-10-17",
71+
"Statement": [
72+
{"Sid": "<THIS IS UNIQE>",
73+
"Action": [
74+
"s3:GetObject",
75+
"s3:PutObject",
76+
"s3:PutObjectAcl"],
77+
"Effect": "Allow",
78+
"Resource": "arn:aws:s3:::<YOUR BUCKET NAME>/*"}]}
79+
```
80+
81+
### Set up lambda with CLI
82+
83+
[Install aws cli](http://docs.aws.amazon.com/cli/latest/userguide/installing.html)
84+
85+
Create new lambda function:
86+
```
87+
aws lambda create-function \
88+
--function-name <name the function> \
89+
--zip-file fileb://code.zip \
90+
--role <role's arn> \ # arn from role with S3 read/write access
91+
--handler s3apt.handler \
92+
--runtime python2.7 \
93+
# Replace '<...>' with environmental variables
94+
--environment Variables='{PUBLIC=<bool>, GPG_KEY=<file>, GPG_PASS=<password>, BUCKET_NAME=<bucket name>, CACHE_PREFIX=<dir>}'
95+
```
96+
97+
### Set up lambda manually
98+
99+
If CLI is not your thing, then you can upload code manaully
100+
101+
Create new lambda function, set handler to **s3apt.lambda_handler**, runtime to **python 2.7**
102+
103+
Upload `code.zip` to lambda function
104+
105+
### The triggers
106+
107+
* Object Created(All), suffix 'deb'
108+
* Object Removed(All), suffix 'deb'
109+
* If you are using certain directory as a repo, set it as prefix
110+
111+
### Set up S3
54112
Make folder in your S3 bucket with the same name as CACHE_PREFIX variable
55113

56114
Upload secret key file to location you specified as GPG_KEY
57115

116+
58117
Upload .deb file to desired folder, lambda function should now keep your repository up to date
59118

60119
## Setting up apt
@@ -77,8 +136,6 @@ sudo apt upgrade
77136

78137
## Notes
79138

80-
.deb, Release and Package files are and should be publicly accessible for previously mentioned method of setting up apt's sources list to work, if you don't want them to be, then change PUBLIC in environment variables to False and refer to szinck's guide [here](http://webscale.plumbing/managing-apt-repos-in-s3-using-lambda)
81-
82-
If somebody tries to inject a malicious deb file in your repo it will be automaticly added to repository. It is your job to make bucket secure enough for this not to happen.!!!
83-
84-
**You should change lambda timeout to 10 seconds or more to make sure that function will work**
139+
* .deb, Release and Package files are and should be publicly accessible for previously mentioned method of setting up apt's sources list to work, if you don't want them to be, then change PUBLIC in environment variables to False and refer to szinck's guide [here](http://webscale.plumbing/managing-apt-repos-in-s3-using-lambda)
140+
* If somebody tries to inject a malicious deb file in your repo it will be automaticly added to repository. It is your job to make bucket secure enough for this not to happen.!!!
141+
* **You should change lambda timeout to more than 10 seconds to make sure that function will work**

deb/s3apt.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def lambda_handler(event, context):
3434
build_release_file(prefix)
3535

3636
#Sign Release file
37-
if not os.environ['GPG_KEY']=='':
37+
if os.environ['GPG_KEY']!='':
3838
sign_release_file(prefix)
3939

4040

@@ -223,24 +223,36 @@ def get_package_index_hash(prefix):
223223
def build_release_file(prefix):
224224
"""
225225
gets info from Package, get the sums and puts them into file
226+
227+
Releasefile layout:
228+
'''
229+
Date: <Day of the week>, DD Mmm YYYY HH:MM:SS UTC
230+
MD5sum:
231+
<md5sum> <(17 - length of size) spaces> <size> Packages
232+
SHA1:
233+
<sha1> <(17 - length of size) spaces> <size> Packages
234+
SHA256:
235+
<sha256> <(17 - length of size) spaces> <size> Packages
236+
'''
226237
"""
227238
s3 = boto3.client('s3')
228239
release_file = ""
229240
s3.download_file(os.environ['BUCKET_NAME'], prefix + "Packages", '/tmp/Packages')
230241
md5, sha1, sha256 = checksums("/tmp/Packages")
231242

232-
time = 'Date: ' + strftime("%a, %d %b %Y %X UTC", gmtime())
243+
date = 'Date: ' + strftime("%a, %d %b %Y %X UTC", gmtime())
233244
stat = os.stat("/tmp/Packages")
234-
release_file = release_file +(time + '\nMD5sum:\n ' + md5)
245+
246+
release_file += (date + '\nMD5sum:\n ' + md5)
235247
for i in range(0,17-len(str(stat.st_size))):
236-
release_file = release_file +(' ')
237-
release_file = release_file +(str(stat.st_size) + ' Packages\nSHA1:\n '+sha1 )
248+
release_file +=(' ')
249+
release_file +=("%d Packages\nSHA1:\n %s" %(stat.st_size, sha1))
238250
for i in range(0,17-len(str(stat.st_size))):
239-
release_file = release_file +(' ')
240-
release_file = release_file +(str(stat.st_size) + ' Packages\nSHA256:\n '+sha256 )
251+
release_file +=(' ')
252+
release_file +=("%d Packages\nSHA256:\n %s" %(stat.st_size, sha256 ))
241253
for i in range(0,17-len(str(stat.st_size))):
242-
release_file = release_file +(' ')
243-
release_file = release_file +(str(stat.st_size) + ' Packages')
254+
release_file +=(' ')
255+
release_file +=('%d Packages' % stat.st_size)
244256

245257
s3 = boto3.resource('s3')
246258

0 commit comments

Comments
 (0)