Skip to content

Commit 789aa02

Browse files
authored
Add support for serving a bucket without a password. (#1)
1 parent 05846d7 commit 789aa02

File tree

4 files changed

+91
-23
lines changed

4 files changed

+91
-23
lines changed

‎README.md‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ S3_ID=your_app_id
4343
S3_KEY=your_key
4444
S3_LINK_TIMEOUT=5
4545
S3_REGION=us-west-2
46+
S3SERVER_LOGIN=password
4647
```
4748

4849
Run the app with `foreman start` to test it out.
@@ -62,19 +63,42 @@ Create a `.env` file in the root of the app with the following contents:
6263
```
6364
RACK_ENV=development
6465
S3SERVER_SECRET_KEY=your_own_made_up_secret_key_for_cookies
65-
GOOGLE_ID=your_app_id
66-
GOOGLE_SECRET=your_google_secret
6766
S3_BUCKET=your_bucket
6867
S3_ID=your_app_id
6968
S3_KEY=your_key
7069
S3_LINK_TIMEOUT=5
7170
S3_REGION=us-west-2
71+
S3SERVER_LOGIN=google
72+
GOOGLE_ID=your_app_id
73+
GOOGLE_SECRET=your_google_secret
7274
```
7375

7476
This file should **never be checked in to version control.**
7577

7678
Run the app with `foreman start` to test it out.
7779

80+
## Anonymous acces
81+
82+
So you want anyone anywhere to be able to grab files from your bucket?
83+
84+
Are you crazy?
85+
86+
Okay, this is supported, but dangerous. You're opening an S3 bucket to the entire world. You're gonna pay pretty high rates for all that data transfer. The links will still expire after your specified timeout, but literally EVERYONE can see everything in your bucket.
87+
88+
Create a `.env` file in the root of the app with the following contents:
89+
90+
```
91+
RACK_ENV=development
92+
S3SERVER_SECRET_KEY=your_own_made_up_secret_key_for_cookies
93+
S3_BUCKET=your_bucket
94+
S3_ID=your_app_id
95+
S3_KEY=your_key
96+
S3_LINK_TIMEOUT=5
97+
S3_REGION=us-west-2
98+
S3SERVER_LOGIN=none
99+
```
100+
101+
That's it. Fire it up with `foreman start` to test things out.
78102

79103
## Production
80104

@@ -90,6 +114,8 @@ to start your app.
90114

91115
See the foreman documentation for [instructions on exporting to Systemd unit files or other startup services](https://github.com/ddollar/foreman/wiki/Exporting-for-production).
92116

117+
Probably should put this behind Nginx with a Let's Encrypt certificate too.
118+
93119
## Running with Docker
94120

95121
You may want to run this server in a container instead. There's a Dockerfile included in the repository.
@@ -113,6 +139,7 @@ docker run -d -p 9292:9292 --name s3server \
113139
-e S3_KEY=your_key \
114140
-e S3_LINK_TIMEOUT=5 \
115141
-e S3_REGION=us-west-2 \
142+
-e S3SERVER_LOGIN=password \
116143
napcs/s3server
117144
```
118145

@@ -122,16 +149,33 @@ If using Google authentication, run the container like this:
122149
docker run -d -p 9292:9292 --name s3server \
123150
-e RACK_ENV=production \
124151
-e S3SERVER_SECRET_KEY=your_own_made_up_secret_key_for_cookies \
152+
-e S3_BUCKET=your_bucket \
153+
-e S3_ID=your_app_id \
154+
-e S3_KEY=your_key \
155+
-e S3_LINK_TIMEOUT=5 \
156+
-e S3_REGION=us-west-2 \
157+
-e S3SERVER_LOGIN=google \
125158
-e GOOGLE_ID=your_app_id \
126159
-e GOOGLE_SECRET=your_google_secret \
160+
napcs/s3server
161+
```
162+
163+
And if you just don't want any authentication for some reason:
164+
165+
```
166+
docker run -d -p 9292:9292 --name s3server \
167+
-e RACK_ENV=production \
168+
-e S3SERVER_SECRET_KEY=your_own_made_up_secret_key_for_cookies \
127169
-e S3_BUCKET=your_bucket \
128170
-e S3_ID=your_app_id \
129171
-e S3_KEY=your_key \
130172
-e S3_LINK_TIMEOUT=5 \
131173
-e S3_REGION=us-west-2 \
174+
-e S3SERVER_LOGIN=none \
132175
napcs/s3server
133176
```
134177

178+
And you're good to go.
135179

136180
Stop the container with
137181

@@ -159,12 +203,21 @@ docker rmi napcs/s3server
159203

160204
## Changelog
161205

162-
* 2017-03-07
206+
* 2017-08-27 (0.3)
207+
* Breaking change:
208+
* To handle Google logins, you'll need to add the new `S3SERVER_LOGIN` variable. Set it to `google`, `password`, or `none` depending on the scheme.
209+
* [Feature] Add support for serving buckets without a password. Use at your own risk.
210+
* Reworked login logic
211+
* CSS tweaks to make audio player 100% wide
212+
* CSS tweak for large screens
213+
214+
* 2017-03-07 (0.2)
163215
* [Bugfix] Name of file was not displaying.
164216
* [Bugfix] US-West region was hardcoded in the ACL lookup.
165217
* [Feature] Added support for password logins with HTTP Basic Auth.
166218
* [Feature] Added HTML audio support for audio files.
167-
* 2017-01-16
219+
220+
* 2017-01-16 (0.1)
168221
* Initial release with Google auth and Docker support.
169222

170223
## License

‎app.rb‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,41 @@ class S3Server < Sinatra::Base
2121
end
2222

2323
get "/login" do
24-
if ENV["GOOGLE_ID"]
25-
redirect "/auth/google_oauth2"
24+
auth = Rack::Auth::Basic::Request.new(request.env)
25+
26+
if auth.provided? and auth.basic? and auth.credentials and can_access_bucket_with_password?(auth.credentials.first, auth.credentials.last)
27+
session[:authenticated] = true
28+
session[:info] = {email: auth.credentials.first, picture: "", name: auth.credentials.first}
29+
redirect "/"
2630
else
27-
auth = Rack::Auth::Basic::Request.new(request.env)
31+
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
32+
halt 401, "Not authorized\n"
33+
end
34+
end
2835

29-
if auth.provided? and auth.basic? and auth.credentials and can_access_bucket_with_password?(auth.credentials.first, auth.credentials.last)
30-
session[:authenticated] = true
31-
session[:info] = {email: auth.credentials.first, picture: "", name: auth.credentials.first}
32-
redirect "/"
36+
def login_check
37+
unless ENV["S3SERVER_LOGIN"] == "none"
38+
return if session[:authenticated]
39+
if ENV["S3SERVER_LOGIN"] == "google"
40+
redirect "/auth/google_oauth2"
3341
else
34-
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
35-
halt 401, "Not authorized\n"
42+
redirect "/login"
3643
end
3744
end
38-
3945
end
4046

4147
get "/" do
42-
redirect "/login" unless session[:authenticated]
48+
login_check
4349
s3 = S3.new ENV["S3_ID"], ENV["S3_KEY"], ENV["S3_BUCKET"], ENV["S3_REGION"]
4450
@data = s3.get_all_objects
4551
erb :index
4652
end
4753

48-
get "/o/:key" do
49-
redirect "/login" unless session[:authenticated]
54+
get "/o/*" do
55+
login_check
56+
key = params[:splat].first
5057
s3 = S3.new ENV["S3_ID"], ENV["S3_KEY"], ENV["S3_BUCKET"], ENV["S3_REGION"]
51-
@data = s3.get_object_data_by_key(params[:key], ENV["S3_LINK_TIMEOUT"])
58+
@data = s3.get_object_data_by_key(key, ENV["S3_LINK_TIMEOUT"])
5259
erb :show
5360
end
5461

‎public/style.css‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ body {
77
width: 98%;
88
}
99

10+
@media (min-width: 1400px) {
11+
.container {
12+
width: 60%;
13+
}
14+
}
15+
1016
header {
1117
background-color: #333;
1218
color: #fff;
@@ -31,4 +37,4 @@ header .login {
3137
width: 50%;
3238
}
3339

34-
video { width: 100% }
40+
video, audio { width: 100% }

‎views/layout.erb‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
<section class="banner">
1515
<h1>S3Server</h1>
1616
</section>
17-
<section class="login">
18-
<img height="16" width="16" alt="Google" src="<%= session[:info][:picture] %>">
19-
<%= session[:info][:name] %>
20-
</section>
17+
<% if session[:info] %>
18+
<section class="login">
19+
<img height="16" width="16" alt="Google" src="<%= session[:info][:picture] %>">
20+
<%= session[:info][:name] %>
21+
</section>
22+
<% end %>
2123
</header>
2224

2325
<main role="main">

0 commit comments

Comments
 (0)