Make nginx kubernetes-ready.
We had to update to use a ubuntu-based image to build nginx into, because (and I
kid you not) alpine linux straight-up ignores your resolv.conf file, meaning any
attempt to use it with kubernetes DNS is doomed to fail. Who thought this was a
good idea?
So we're using a bloated image instead. Oh well.
We also are running a wrapper script instead of nginx directly, so we can inject
the JWT_SECRET environment variable based on a kubernetes secret file.
We define the secret file (using a placeholder secret, obvs) so that
future-Paddy can remember what the hell it looks like, when he inevitably loses
the file and needs to sin up a new cluster. Or whatever.
Finally, we updated the token expiration error message to be in an errors array,
as God (and our API conventions) intended.
1 ------------------------------------------------------------------------------------------------------------------------
3 ------------------------------------------------------------------------------------------------------------------------
5 function divide_string( str, max, fillChar )
6 fillChar = fillChar or ""
12 table.insert( result, str:sub( start, i ) )
15 table.insert( result, str:sub( start, i ) )
22 function number_to_bit( num, length )
26 local rest = math.fmod( num, 2 )
27 table.insert( bits, rest )
28 num = ( num - rest ) / 2
31 while #bits < length do
32 table.insert( bits, "0" )
35 return string.reverse( table.concat( bits ) )
38 ------------------------------------------------------------------------------------------------------------------------
42 ------------------------------------------------------------------------------------------------------------------------
43 -- base2(bitfield) decode and encode function
44 ------------------------------------------------------------------------------------------------------------------------
46 local bitMap = { o = "0", i = "1", l = "1" }
48 function basexx.from_bit( str )
49 str = string.lower( str )
50 str = str:gsub( '[ilo]', function( c ) return bitMap[ c ] end )
51 return ( str:gsub( '........', function ( cc )
52 return string.char( tonumber( cc, 2 ) )
56 function basexx.to_bit( str )
57 return ( str:gsub( '.', function ( c )
58 local byte = string.byte( c )
61 table.insert( bits, byte % 2 )
62 byte = math.floor( byte / 2 )
64 return table.concat( bits ):reverse()
68 ------------------------------------------------------------------------------------------------------------------------
69 -- base16(hex) decode and encode function
70 ------------------------------------------------------------------------------------------------------------------------
72 function basexx.from_hex( str )
73 return ( str:gsub( '..', function ( cc )
74 return string.char( tonumber( cc, 16 ) )
78 function basexx.to_hex( str )
79 return ( str:gsub( '.', function ( c )
80 return string.format('%02X', string.byte( c ) )
84 ------------------------------------------------------------------------------------------------------------------------
85 -- generic function to decode and encode base32/base64
86 ------------------------------------------------------------------------------------------------------------------------
88 local function from_basexx( str, alphabet, bits )
91 local c = string.sub( str, i, i )
93 local index = string.find( alphabet, c )
94 table.insert( result, number_to_bit( index - 1, bits ) )
98 local value = table.concat( result )
99 local pad = #value % 8
100 return basexx.from_bit( string.sub( value, 1, #value - pad ) )
103 local function to_basexx( str, alphabet, bits, pad )
104 local bitString = basexx.to_bit( str )
106 local chunks = divide_string( bitString, bits )
108 for key,value in ipairs( chunks ) do
109 if ( #value < bits ) then
110 value = value .. string.rep( '0', bits - #value )
112 local pos = tonumber( value, 2 ) + 1
113 table.insert( result, alphabet:sub( pos, pos ) )
116 table.insert( result, pad )
117 return table.concat( result )
120 ------------------------------------------------------------------------------------------------------------------------
121 -- rfc 3548: http://www.rfc-editor.org/rfc/rfc3548.txt
122 ------------------------------------------------------------------------------------------------------------------------
124 local base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
126 function basexx.from_base32( str )
127 return from_basexx( string.upper( str ), base32Alphabet, 5 )
130 function basexx.to_base32( str )
131 return to_basexx( str, base32Alphabet, 5, ({ '', '======', '====', '===', '=' })[ #str % 5 + 1 ] )
134 ------------------------------------------------------------------------------------------------------------------------
135 -- crockford: http://www.crockford.com/wrmg/base32.html
136 ------------------------------------------------------------------------------------------------------------------------
138 local crockfordAlphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
139 local crockfordMap = { O = "0", I = "1", L = "1", U = "V" }
141 function basexx.from_crockford( str )
142 str = string.upper( str )
143 str = str:gsub( '[ILOU]', function( c ) return crockfordMap[ c ] end )
144 return from_basexx( str, crockfordAlphabet, 5 )
147 function basexx.to_crockford( str )
148 return to_basexx( str, crockfordAlphabet, 5, "" )
151 ------------------------------------------------------------------------------------------------------------------------
152 -- base64 decode and encode function
153 ------------------------------------------------------------------------------------------------------------------------
155 local base64Alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
157 function basexx.from_base64( str )
158 return from_basexx( str, base64Alphabet, 6 )
161 function basexx.to_base64( str )
162 return to_basexx( str, base64Alphabet, 6, ({ '', '==', '=' })[ #str % 3 + 1 ] )
165 ------------------------------------------------------------------------------------------------------------------------