| 1 | package Langertha::Knarr::Stack; | ||
| 2 | # ABSTRACT: The routing parts of a running Knarr, built from one config | ||
| 3 | our $VERSION = '1.103'; | ||
| 4 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use JSON::MaybeXS (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Langertha::Knarr::Router; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Langertha::Knarr::Handler::Passthrough; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Langertha::Knarr::Handler::Router; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Langertha::Knarr::Handler::Tracing; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Langertha::Knarr::Handler::RequestLog; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Langertha::Knarr::Handler::TokenRoutes; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Langertha::Knarr::Authorizer::Token; | ||
| 13 | use Langertha::Knarr::TokenStore::Pg; | ||
| 14 | |||
| 15 | |||
| 16 | has config => ( | ||
| 17 | is => 'ro', | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | has loop => ( | ||
| 22 | is => 'ro', | ||
| 23 | ); | ||
| 24 | |||
| 25 | has tracing => ( | ||
| 26 | is => 'ro', | ||
| 27 | isa => 'Maybe[Object]', | ||
| 28 | default => sub { undef }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has request_log => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'Maybe[Object]', | ||
| 34 | default => sub { undef }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has router => ( | ||
| 38 | is => 'ro', | ||
| 39 | lazy => 1, | ||
| 40 | builder => '_build_router', | ||
| 41 | ); | ||
| 42 | |||
| 43 | has raw_passthrough => ( | ||
| 44 | is => 'ro', | ||
| 45 | lazy => 1, | ||
| 46 | builder => '_build_raw_passthrough', | ||
| 47 | ); | ||
| 48 | |||
| 49 | has token_store => ( | ||
| 50 | is => 'ro', | ||
| 51 | isa => 'Maybe[Object]', | ||
| 52 | lazy => 1, | ||
| 53 | builder => '_build_token_store', | ||
| 54 | ); | ||
| 55 | |||
| 56 | has authorizer => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => 'Maybe[Object]', | ||
| 59 | lazy => 1, | ||
| 60 | builder => '_build_authorizer', | ||
| 61 | ); | ||
| 62 | |||
| 63 | has handler => ( | ||
| 64 | is => 'ro', | ||
| 65 | lazy => 1, | ||
| 66 | builder => '_build_handler', | ||
| 67 | ); | ||
| 68 | |||
| 69 | # The classes the stack is made of, named once each (override points). | ||
| 70 | sub router_class { 'Langertha::Knarr::Router' } | ||
| 71 | sub token_routes_class { 'Langertha::Knarr::Handler::TokenRoutes' } | ||
| 72 | sub authorizer_class { 'Langertha::Knarr::Authorizer::Token' } | ||
| 73 | sub token_store_class { 'Langertha::Knarr::TokenStore::Pg' } | ||
| 74 | sub passthrough_class { 'Langertha::Knarr::Handler::Passthrough' } | ||
| 75 | sub router_handler_class { 'Langertha::Knarr::Handler::Router' } | ||
| 76 | sub tracing_handler_class { 'Langertha::Knarr::Handler::Tracing' } | ||
| 77 | sub request_log_handler_class { 'Langertha::Knarr::Handler::RequestLog' } | ||
| 78 | |||
| 79 | sub _build_router { | ||
| 80 | my ($self) = @_; | ||
| 81 | return $self->router_class->new( config => $self->config ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _build_token_store { | ||
| 85 | my ($self) = @_; | ||
| 86 | my $auth = $self->config->token_auth or return undef; | ||
| 87 | return $self->token_store_class->new( | ||
| 88 | dsn => $auth->{dsn}, | ||
| 89 | relation => $auth->{relation}, | ||
| 90 | listen => $auth->{listen}, | ||
| 91 | columns => $auth->{columns}, | ||
| 92 | ( defined $auth->{user} ? ( user => $auth->{user} ) : () ), | ||
| 93 | ( defined $auth->{password_env} ? ( password_env => $auth->{password_env} ) : () ), | ||
| 94 | ( $self->loop ? ( loop => $self->loop ) : () ), | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _build_authorizer { | ||
| 99 | my ($self) = @_; | ||
| 100 | my $auth = $self->config->token_auth or return undef; | ||
| 101 | return $self->authorizer_class->new( | ||
| 102 | store => $self->token_store, | ||
| 103 | match_model => $auth->{match_model}, | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _build_raw_passthrough { | ||
| 108 | my ($self) = @_; | ||
| 109 | my $config = $self->config; | ||
| 110 | # Token-checked requests never go through the raw passthrough: it would | ||
| 111 | # hand the client's token upstream. | ||
| 112 | return undef if $config->token_auth; | ||
| 113 | my $upstreams = $config->passthrough; | ||
| 114 | return undef unless $upstreams && %$upstreams; | ||
| 115 | return $self->passthrough_class->new( | ||
| 116 | upstreams => $upstreams, | ||
| 117 | ( $self->loop ? ( loop => $self->loop ) : () ), | ||
| 118 | timeout => $config->upstream_timeout, | ||
| 119 | stall_timeout => $config->upstream_stall_timeout, | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _build_handler { | ||
| 124 | my ($self) = @_; | ||
| 125 | my $passthrough = $self->raw_passthrough; | ||
| 126 | my $handler = $self->config->token_auth | ||
| 127 | ? $self->token_routes_class->new( | ||
| 128 | routes => $self->config->routes, | ||
| 129 | router => $self->router, | ||
| 130 | ) | ||
| 131 | : $self->router_handler_class->new( | ||
| 132 | router => $self->router, | ||
| 133 | ( $passthrough ? ( passthrough => $passthrough ) : () ), | ||
| 134 | ); | ||
| 135 | $handler = $self->tracing_handler_class->new( | ||
| 136 | wrapped => $handler, | ||
| 137 | tracing => $self->tracing, | ||
| 138 | ) if $self->tracing; | ||
| 139 | $handler = $self->request_log_handler_class->new( | ||
| 140 | wrapped => $handler, | ||
| 141 | request_log => $self->request_log, | ||
| 142 | ) if $self->request_log; | ||
| 143 | return $handler; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub auth_token { | ||
| 147 | my ($self) = @_; | ||
| 148 | return $self->config->has_proxy_api_key ? $self->config->proxy_api_key : undef; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub with_config { | ||
| 152 | my ($self, $config, %over) = @_; | ||
| 153 | my $same_auth = $self->_same_token_auth($config); | ||
| 154 | return ref($self)->new( | ||
| 155 | ( $same_auth && $self->authorizer | ||
| 156 | ? ( authorizer => $self->authorizer, token_store => $self->token_store ) : () ), | ||
| 157 | config => $config, | ||
| 158 | loop => $self->loop, | ||
| 159 | tracing => ( exists $over{tracing} ? $over{tracing} : $self->tracing ), | ||
| 160 | request_log => $self->request_log, | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Whether $config asks for the very token check this stack has. | ||
| 165 | sub _same_token_auth { | ||
| 166 | my ($self, $config) = @_; | ||
| 167 | my ( $mine, $next ) = ( $self->config->token_auth, $config->token_auth ); | ||
| 168 | return 0 unless $mine && $next; | ||
| 169 | my $json = JSON::MaybeXS->new( canonical => 1 ); | ||
| 170 | return $json->encode($mine) eq $json->encode($next) ? 1 : 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub build { | ||
| 174 | my ($self) = @_; | ||
| 175 | $self->$_ for qw( router raw_passthrough token_store authorizer handler ); | ||
| 176 | return $self; | ||
| 177 | } | ||
| 178 | |||
| 179 | __PACKAGE__->meta->make_immutable; | ||
| 180 | 1; | ||
| 181 | |||
| 182 | __END__ | ||