forked from parabuzzle/craneoperator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rb
243 lines (205 loc) · 6 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require 'oj'
require 'pry'
require 'erb'
require 'time'
require 'sinatra/base'
require 'sinatra/cross_origin'
require './lib/config.rb'
require './lib/helpers.rb'
class CraneOp < Sinatra::Base
register Sinatra::CrossOrigin
include Helpers
configure do
enable :cross_origin
enable :sessions
mime_type :javascript, 'application/javascript'
mime_type :javascript, 'text/javascript'
set :logging, true
set :static, true
set :allow_origin, :any
set :allow_methods, [:get, :post, :options]
set :allow_credentials, true
set :max_age, "1728000"
set :expose_headers, ['Content-Type']
set :json_encoder, :to_json
set :session_secret, Configuration.new.session_secret
end
def conf
return Configuration.new
end
## Basic Auth ##
if Configuration.new.username
config = Configuration.new
use Rack::Auth::Basic, "Please Authenticate to View" do |username, password|
username == config.username and password == ( config.password || '' )
end
end
## Registry API Methods ##
def fetch_catalog(next_string=nil)
query={n: 100, last: next_string}
json = get("/v2/_catalog", conf, session, query: query)
if json['errors']
return json
end
return [] if json['repositories'].nil?
repos = []
repos += json['repositories']
unless json['repositories'].empty?
repos += fetch_catalog(repos.last)
end
return repos
end
def containers(filter=nil)
repos = fetch_catalog
return repos if repos.is_a?(Hash) && repos['errors']
if filter
return repos.select{ |i| i.match(/#{filter}.*/)}
end
return repos
end
def container_tags(repo, filter=nil)
json = get("/v2/#{repo}/tags/list", conf, session)
return nil if json['tags'].nil?
tags = json['tags'] || []
if filter
return sort_versions(tags.select{ |i| i.match(/#{filter}.*/)}).reverse
end
sort_versions(tags).reverse
end
def container_info(repo, manifest)
json = get("/v2/#{repo}/manifests/#{manifest}", conf, session)
# Add extra fields for easy display
if json['history']
history = json['history'].shift
json['information'] = Oj.load(history['v1Compatibility'])
json['layer_info'] = []
json['history'].each do |i|
json['layer_info'] << Oj.load(i['v1Compatibility'])
end
json['layer_info'].reverse!
created_at = Time.parse(json['information']['created'])
json['information']['created_formatted'] = created_at.to_s
json['information']['created_millis'] = (created_at.to_f * 1000).to_i
end
return json
end
def fetch_digest(repo, manifest)
response = http_head("/v2/#{repo}/manifests/#{manifest}", conf, session, headers: { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json'})
return response.headers["docker-content-digest"]
end
def image_delete(repo, manifest)
digest = fetch_digest(repo, manifest)
return http_delete("/v2/#{repo}/manifests/#{digest}", conf, session, headers: { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json'})
end
## Endpoints ##
get '/api' do
return "API Version #{conf.version}"
end
get '/api/containers' do
content_type :json
repos = containers(params[:filter])
if !repos.is_a?(Array)
halt 401, {'content-type' => 'application/json'}, {'message' => "Registry requires authentication"}.to_json
end
repos.to_json
end
get '/api/tags/*' do |container|
content_type :json
tags = container_tags(container, params[:filter])
halt 404 if tags.nil?
tags.to_json
end
post '/api/login' do
content_type :json
params = Oj.load(request.body.read)
session.delete(:username)
session.delete(:password)
username = params['username']
password = params['password']
if check_login(conf, session, username, password)
session[:username] = username
session[:password] = password
return {status: "success"}.to_json
end
halt 401, {error: "credentials are wrong"}.to_json
end
get '/logout' do
session.destroy
redirect '/'
end
get /api\/containers\/(.*\/)(.*)/ do |container, tag|
# This is here because we need to handle slashes in container names
container.chop!
content_type :json
info = container_info(container, tag)
halt 404 if info['errors']
halt 404 if info['fsLayers'].nil?
info.to_json
end
get '/api/registryinfo' do
content_type :json
info = {
host: conf.registry_host,
public_url: conf.registry_public_url,
port: conf.registry_port,
protocol: conf.registry_protocol,
ssl_verify: conf.ssl_verify,
delete_allowed: conf.delete_allowed,
login_allowed: conf.login_allowed,
title: conf.title,
}
if session[:username]
info[:username] = session[:username]
end
info.to_json
end
delete /api\/containers\/(.*\/)(.*)/ do |container, tag|
halt 404 unless to_bool(conf.delete_allowed)
container.chop!
response = image_delete( container, tag )
headers = response.headers
response.body
end
# React app endpoints
[
"/containers/*",
"/containers",
"/login",
"/login/",
"/",
].each do |path|
get path do
erb :index, locals: { title: conf.title}
end
end
# Error Handlers
error do
File.read(File.join('public', '500.html'))
end
not_found do
status 404
File.read(File.join('public', '404.html'))
end
# Debug endpoints
if Configuration.new.debug
get '/api/session' do
session.to_hash.to_json
end
get '/api/config' do
conf.to_hash.to_json
end
get '/api/login' do
content_type :json
session.delete(:username)
session.delete(:password)
username = params['username']
password = params['password']
if check_login(conf, session, username, password)
session[:username] = username
session[:password] = password
return {status: "success"}.to_json
end
halt 401, {error: "credentials are wrong"}.to_json
end
end
end