« Repositorio/Repository/Yum.pm
↑ Summary
Repositorio/Server/Docker/Mojolicious/Plugin/DockerSession.pm »
| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Repositorio::Server::Docker::Image; | ||
| 8 | |||
| 9 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Spec; | ||
| 12 | use File::Path 'make_path', 'remove_tree'; | ||
| 13 | use File::Basename 'basename'; | ||
| 14 | require IO::All; | ||
| 15 | use JSON::XS; | ||
| 16 | use MIME::Base64; | ||
| 17 | |||
| 18 | our $VERSION = '1.2.1'; # VERSION | ||
| 19 | |||
| 20 | sub put_image { | ||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 24 | my $image_dir = | ||
| 25 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 26 | my $image_json = File::Spec->catfile( $image_dir, "image.json" ); | ||
| 27 | |||
| 28 | make_path $image_dir; | ||
| 29 | |||
| 30 | open( my $fh, ">", $image_json ) or die($!); | ||
| 31 | print $fh $self->req->body; | ||
| 32 | close($fh); | ||
| 33 | |||
| 34 | eval { | ||
| 35 | my $ref = decode_json $self->req->body; | ||
| 36 | if ( exists $ref->{parent} ) { | ||
| 37 | symlink File::Spec->catfile( $repo_dir, "images", $ref->{parent} ), | ||
| 38 | File::Spec->catfile( $image_dir, 'parent' ); | ||
| 39 | } | ||
| 40 | 1; | ||
| 41 | } or do { | ||
| 42 | print STDERR ">> ERR> $@\n"; | ||
| 43 | }; | ||
| 44 | |||
| 45 | $self->render( text => "true" ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub put_image_layer { | ||
| 49 | my ($self) = @_; | ||
| 50 | |||
| 51 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 52 | my $image_dir = | ||
| 53 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 54 | my $image_layer = File::Spec->catfile( $image_dir, "image.layer" ); | ||
| 55 | |||
| 56 | my $chunk = $self->req->body; | ||
| 57 | |||
| 58 | open( my $fh, ">", $image_layer ) or die($!); | ||
| 59 | print $fh $chunk; | ||
| 60 | close($fh); | ||
| 61 | |||
| 62 | $self->render( text => 'true' ); | ||
| 63 | } | ||
| 64 | |||
| 65 | # TODO: implement checksuming | ||
| 66 | sub put_image_checksum { | ||
| 67 | my ($self) = @_; | ||
| 68 | |||
| 69 | my $name = $self->param("name"); | ||
| 70 | |||
| 71 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 72 | my $image_dir = | ||
| 73 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 74 | my $chksum_file = File::Spec->catfile( $image_dir, "payload.sha256" ); | ||
| 75 | |||
| 76 | #print STDERR "($name) got chksum> " | ||
| 77 | # . $self->req->headers->header('X-Docker-Checksum') . "\n"; | ||
| 78 | #print STDERR "($name) got chksum payload> " | ||
| 79 | # . $self->req->headers->header('X-Docker-Checksum-Payload') . "\n"; | ||
| 80 | |||
| 81 | open( my $fh, ">", $chksum_file ) or die($!); | ||
| 82 | print $fh $self->req->headers->header('X-Docker-Checksum-Payload'); | ||
| 83 | close($fh); | ||
| 84 | |||
| 85 | $self->render( text => 'true' ); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub get_image_ancestry { | ||
| 89 | my ($self) = @_; | ||
| 90 | |||
| 91 | my $requested_file = $self->req->url; | ||
| 92 | $self->app->log->debug("Requested-File: $requested_file"); | ||
| 93 | my $orig_url = $self->repo->{url}; | ||
| 94 | $orig_url =~ s/\/$//; | ||
| 95 | $self->app->log->debug("Upstream-URL: $orig_url"); | ||
| 96 | |||
| 97 | $requested_file =~ s/^\///; | ||
| 98 | my $orig_file_url = $orig_url . "/" . $requested_file; | ||
| 99 | $self->app->log->debug("Orig-File-URL: $orig_file_url"); | ||
| 100 | |||
| 101 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 102 | my $image_dir = | ||
| 103 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 104 | |||
| 105 | my @ids = ( $self->param("name") ); | ||
| 106 | my $parent_id_file = File::Spec->catfile( $image_dir, "parent" ); | ||
| 107 | if ( -l $parent_id_file ) { | ||
| 108 | my $parent = readlink $parent_id_file; | ||
| 109 | while ($parent) { | ||
| 110 | my $parent_id = basename $parent; | ||
| 111 | push @ids, $parent_id; | ||
| 112 | $parent = | ||
| 113 | readlink File::Spec->catfile( $image_dir, "..", $parent_id, "parent" ); | ||
| 114 | } | ||
| 115 | |||
| 116 | $self->render( json => \@ids ); | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | $self->_auth_upstream(); | ||
| 120 | my $upstream_file = File::Spec->catfile( $image_dir, "endpoint.data" ); | ||
| 121 | $self->app->log->debug("Looking for upstream file: $upstream_file"); | ||
| 122 | if ( -f $upstream_file ) { | ||
| 123 | my ($upstream_host) = eval { local (@ARGV) = ($upstream_file); <>; }; | ||
| 124 | $orig_file_url =~ s"^(http|https)://([^/]+)/"$1://$upstream_host/"; | ||
| 125 | $self->app->log->debug("Rewrite Upstream-URL: $orig_file_url"); | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->proxy_to( | ||
| 129 | $orig_file_url, | ||
| 130 | sub { | ||
| 131 | my ( $c, $tx ) = @_; | ||
| 132 | $c->app->log->debug("Got data from upstream..."); | ||
| 133 | $c->app->log->debug("Writing ancestry (parent) file: $parent_id_file"); | ||
| 134 | my $ref = $tx->res->json; | ||
| 135 | my @ids = @{$ref}; | ||
| 136 | shift @ids; # first one is the directory itself. | ||
| 137 | my $child_dir = File::Spec->catfile( $image_dir, "parent" ); | ||
| 138 | for my $parent_id (@ids) { | ||
| 139 | my $link_target = | ||
| 140 | File::Spec->catdir( $repo_dir, "images", $parent_id ); | ||
| 141 | my $link_name = $child_dir; | ||
| 142 | symlink $link_target, $link_name; | ||
| 143 | |||
| 144 | $child_dir = File::Spec->catfile( $link_target, "parent" ); | ||
| 145 | } | ||
| 146 | }, | ||
| 147 | sub { | ||
| 148 | my ( $c, $tx ) = @_; | ||
| 149 | $self->_fix_docker_headers($tx); | ||
| 150 | }, | ||
| 151 | { | ||
| 152 | 'Authorization' => 'Token ' . $self->stash('upstream_docker_token'), | ||
| 153 | } | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | sub get_image_layer { | ||
| 159 | my ($self) = @_; | ||
| 160 | |||
| 161 | my $requested_file = $self->req->url; | ||
| 162 | $self->app->log->debug("Requested-File: $requested_file"); | ||
| 163 | my $orig_url = $self->repo->{url}; | ||
| 164 | $orig_url =~ s/\/$//; | ||
| 165 | $self->app->log->debug("Upstream-URL: $orig_url"); | ||
| 166 | |||
| 167 | $requested_file =~ s/^\///; | ||
| 168 | my $orig_file_url = $orig_url . "/" . $requested_file; | ||
| 169 | $self->app->log->debug("Orig-File-URL: $orig_file_url"); | ||
| 170 | |||
| 171 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 172 | my $image_dir = | ||
| 173 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 174 | my $layer_file = File::Spec->catfile( $image_dir, "image.layer" ); | ||
| 175 | $self->app->log->debug("Layer-File: $layer_file"); | ||
| 176 | |||
| 177 | if ( -f $layer_file ) { | ||
| 178 | $self->res->headers->add( 'Content-Type', 'application/octet-stream' ); | ||
| 179 | |||
| 180 | #$self->render(data => IO::All->new($layer_file)->slurp); | ||
| 181 | $self->render_file( filepath => $layer_file ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $self->_auth_upstream(); | ||
| 185 | my $upstream_file = File::Spec->catfile( $image_dir, "endpoint.data" ); | ||
| 186 | $self->app->log->debug("Looking for upstream file: $upstream_file"); | ||
| 187 | if ( -f $upstream_file ) { | ||
| 188 | my ($upstream_host) = eval { local (@ARGV) = ($upstream_file); <>; }; | ||
| 189 | $orig_file_url =~ s"^(http|https)://([^/]+)/"$1://$upstream_host/"; | ||
| 190 | $self->app->log->debug("Rewrite Upstream-URL: $orig_file_url"); | ||
| 191 | } | ||
| 192 | |||
| 193 | $self->proxy_to( | ||
| 194 | $orig_file_url, | ||
| 195 | sub { | ||
| 196 | my ( $c, $tx ) = @_; | ||
| 197 | $c->app->log->debug("Got data from upstream..."); | ||
| 198 | $c->app->log->debug("Writing layer file: $layer_file"); | ||
| 199 | open my $fh, '>', $layer_file or die($!); | ||
| 200 | binmode $fh; | ||
| 201 | print $fh $tx->res->body; | ||
| 202 | close $fh; | ||
| 203 | }, | ||
| 204 | sub { | ||
| 205 | my ( $c, $tx ) = @_; | ||
| 206 | $self->_fix_docker_headers($tx); | ||
| 207 | }, | ||
| 208 | { | ||
| 209 | 'Authorization' => 'Token ' . $self->stash('upstream_docker_token'), | ||
| 210 | } | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | sub get_image { | ||
| 216 | my ($self) = @_; | ||
| 217 | |||
| 218 | my $requested_file = $self->req->url; | ||
| 219 | $self->app->log->debug("Requested-File: $requested_file"); | ||
| 220 | my $orig_url = $self->repo->{url}; | ||
| 221 | $orig_url =~ s/\/$//; | ||
| 222 | $self->app->log->debug("Upstream-URL: $orig_url"); | ||
| 223 | |||
| 224 | $requested_file =~ s/^\///; | ||
| 225 | my $orig_file_url = $orig_url . "/" . $requested_file; | ||
| 226 | $self->app->log->debug("Orig-File-URL: $orig_file_url"); | ||
| 227 | |||
| 228 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 229 | my $image_dir = | ||
| 230 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 231 | $self->app->log->debug("Found image directory: $image_dir"); | ||
| 232 | |||
| 233 | my $image_json = File::Spec->catfile( $image_dir, "image.json" ); | ||
| 234 | my $chksum_file = File::Spec->catfile( $image_dir, "payload.sha256" ); | ||
| 235 | |||
| 236 | if ( -f $image_json ) { | ||
| 237 | my $content = IO::All->new($image_json)->slurp; | ||
| 238 | my $chksum = IO::All->new($chksum_file)->slurp; | ||
| 239 | $self->res->headers->add( 'X-Docker-Payload-Checksum', $chksum ); | ||
| 240 | $self->res->headers->add( 'Content-Type', 'application/json' ); | ||
| 241 | |||
| 242 | return $self->render( text => $content ); | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | $self->_auth_upstream(); | ||
| 246 | my $upstream_file = File::Spec->catfile( $image_dir, "endpoint.data" ); | ||
| 247 | $self->app->log->debug("Looking for upstream file: $upstream_file"); | ||
| 248 | if ( -f $upstream_file ) { | ||
| 249 | my ($upstream_host) = eval { local (@ARGV) = ($upstream_file); <>; }; | ||
| 250 | $orig_file_url =~ s"^(http|https)://([^/]+)/"$1://$upstream_host/"; | ||
| 251 | $self->app->log->debug("Rewrite Upstream-URL: $orig_file_url"); | ||
| 252 | } | ||
| 253 | |||
| 254 | $self->proxy_to( | ||
| 255 | $orig_file_url, | ||
| 256 | sub { | ||
| 257 | my ( $c, $tx ) = @_; | ||
| 258 | $c->app->log->debug("Got data from upstream..."); | ||
| 259 | open my $fh, '>', $image_json or die($!); | ||
| 260 | print $fh $tx->res->body; | ||
| 261 | close $fh; | ||
| 262 | |||
| 263 | open my $fh_p, '>', $chksum_file or die($!); | ||
| 264 | print $fh_p $tx->res->headers->header('X-Docker-Payload-Checksum'); | ||
| 265 | close $fh_p; | ||
| 266 | }, | ||
| 267 | sub { | ||
| 268 | my ( $c, $tx ) = @_; | ||
| 269 | $self->_fix_docker_headers($tx); | ||
| 270 | }, | ||
| 271 | { | ||
| 272 | 'Authorization' => 'Token ' . $self->stash('upstream_docker_token'), | ||
| 273 | } | ||
| 274 | ); | ||
| 275 | } | ||
| 276 | |||
| 277 | #return $self->render( json => { error => "Image not found" }, status => 404 ); | ||
| 278 | } | ||
| 279 | |||
| 280 | sub _fix_docker_headers { | ||
| 281 | |||
| 282 | my ( $self, $tx ) = @_; | ||
| 283 | |||
| 284 | my $docker_endpoint = $tx->res->headers->header('X-Docker-Endpoints'); | ||
| 285 | $tx->res->headers->remove('X-Docker-Endpoints'); | ||
| 286 | $tx->res->headers->remove('X-Docker-Token'); | ||
| 287 | $tx->res->headers->add( 'R-Docker-Endpoints', $docker_endpoint ) | ||
| 288 | if $docker_endpoint; | ||
| 289 | |||
| 290 | if ( $self->res->headers->header('WWW-Authenticate') ) { | ||
| 291 | $tx->res->headers->add( 'WWW-Authenticate', | ||
| 292 | $self->res->headers->header('WWW-Authenticate') ); | ||
| 293 | } | ||
| 294 | |||
| 295 | if ( $self->res->headers->header('X-Docker-Token') ) { | ||
| 296 | $tx->res->headers->add( 'X-Docker-Token', | ||
| 297 | $self->res->headers->header('X-Docker-Token') ); | ||
| 298 | } | ||
| 299 | |||
| 300 | $tx->res->headers->add( 'X-Docker-Endpoints' => $self->req->headers->host ); | ||
| 301 | $tx->res->headers->add( 'Pragma' => 'no-cache' ); | ||
| 302 | $tx->res->headers->add( 'Expires' => '-1' ); | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | sub _auth_upstream { | ||
| 307 | my ($self) = @_; | ||
| 308 | my $docker_token = $self->stash('upstream_docker_token'); | ||
| 309 | if ($docker_token) { | ||
| 310 | $self->app->log->debug("Docker Token: $docker_token"); | ||
| 311 | } | ||
| 312 | else { | ||
| 313 | $self->app->log->debug("Docker Token: no token found in session."); | ||
| 314 | $self->app->log->debug("Need to authenticate on upstream docker registry."); | ||
| 315 | |||
| 316 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 317 | my $image_dir = | ||
| 318 | File::Spec->catdir( $repo_dir, "images", $self->param("name") ); | ||
| 319 | my $library = File::Spec->catfile( $image_dir, "library.data" ); | ||
| 320 | if ( !-f $library ) { | ||
| 321 | $self->app->log->error("Can't find upstream library url."); | ||
| 322 | die "Error finding upstream library url."; | ||
| 323 | } | ||
| 324 | my $registry_url = IO::All->new($library)->slurp; | ||
| 325 | $self->app->log->debug("Got upstream registry url: $registry_url"); | ||
| 326 | |||
| 327 | my $base64_auth_string = MIME::Base64::encode_base64( | ||
| 328 | $self->repo->{upstream_user} . ":" . $self->repo->{upstream_password} ); | ||
| 329 | |||
| 330 | # need to create a new object, so there is no cookies from proxy requests in it | ||
| 331 | my $ua = Mojo::UserAgent->new; | ||
| 332 | $ua->max_redirects(5); | ||
| 333 | |||
| 334 | if ( $self->repo->{ca} ) { | ||
| 335 | $ua->ca( $self->repo->{ca} ); | ||
| 336 | } | ||
| 337 | if ( $self->repo->{key} ) { | ||
| 338 | $ua->key( $self->repo->{key} ); | ||
| 339 | } | ||
| 340 | if ( $self->repo->{cert} ) { | ||
| 341 | $ua->cert( $self->repo->{cert} ); | ||
| 342 | } | ||
| 343 | |||
| 344 | my $tx = $ua->get( | ||
| 345 | $registry_url, | ||
| 346 | |||
| 347 | { # some custom headers to get the docker token, so that we can | ||
| 348 | # authenticate against the image servers. | ||
| 349 | 'X-Docker-Token' => 'true', | ||
| 350 | 'Authorization' => "Basic $base64_auth_string", | ||
| 351 | }, | ||
| 352 | |||
| 353 | ); | ||
| 354 | |||
| 355 | if ( $tx->success ) { | ||
| 356 | my $docker_token = $tx->res->headers->header('X-Docker-Token') || ""; | ||
| 357 | if ( !$docker_token ) { | ||
| 358 | $self->app->log->error( | ||
| 359 | "Authentication successfull but can't find a token."); | ||
| 360 | $self->app->log->debug( Dumper($tx) ); | ||
| 361 | } | ||
| 362 | $self->app->log->debug("Got my docker token: $docker_token"); | ||
| 363 | $self->stash( 'upstream_docker_token', $docker_token ); | ||
| 364 | } | ||
| 365 | else { | ||
| 366 | $self->app->log->error( | ||
| 367 | "Can't authenticate to upstream docker registry: $registry_url"); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | 1; |