11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ #
14
15
15
16
require 'kubernetes/configuration'
16
17
require 'kubernetes/config/error'
@@ -19,27 +20,29 @@ module Kubernetes
19
20
# The InClusterConfig class represents configuration for authn/authz in a
20
21
# Kubernetes cluster.
21
22
class InClusterConfig
22
- # rubocop:disable LineLength
23
+ # rubocop:disable Metrics/ LineLength
23
24
SERVICE_HOST_ENV_NAME = 'KUBERNETES_SERVICE_HOST' . freeze
24
25
SERVICE_PORT_ENV_NAME = 'KUBERNETES_SERVICE_PORT' . freeze
25
26
SERVICE_TOKEN_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/token' . freeze
26
27
SERVICE_CA_CERT_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' . freeze
27
- # rubocop:enable LineLength
28
+ TOKEN_REFRESH_PERIOD = 60 # 1 minute
29
+ # rubocop:enable Metrics/LineLength
28
30
29
31
attr_accessor :host
30
32
attr_accessor :port
31
33
attr_accessor :token
34
+ attr_accessor :token_expires_at
32
35
33
36
def validate
34
37
unless ( self . host = env [ SERVICE_HOST_ENV_NAME ] ) &&
35
38
( self . port = env [ SERVICE_PORT_ENV_NAME ] )
36
39
raise ConfigError , 'Service host/port is not set'
37
40
end
38
41
39
- # rubocop:disable LineLength
42
+ # rubocop:disable Metrics/ LineLength
40
43
raise ConfigError , 'Service token file does not exists' unless File . file? ( token_file )
41
44
raise ConfigError , 'Service token file does not exists' unless File . file? ( ca_cert )
42
- # rubocop:enable LineLength
45
+ # rubocop:enable Metrics/ LineLength
43
46
end
44
47
45
48
def self . in_cluster?
@@ -62,19 +65,42 @@ def token_file
62
65
@token_file
63
66
end
64
67
68
+ def token_refresh_period
69
+ @token_refresh_period ||= TOKEN_REFRESH_PERIOD
70
+ @token_refresh_period
71
+ end
72
+
65
73
def load_token
66
74
File . open ( token_file ) do |io |
67
75
self . token = io . read . chomp
76
+ self . token_expires_at = Time . now + token_refresh_period
68
77
end
69
78
end
70
79
71
- def configure ( configuration )
80
+ # rubocop:disable Metrics/AbcSize
81
+ def configure ( configuration , try_refresh_token : true )
72
82
validate
73
83
load_token
74
84
configuration . api_key [ 'authorization' ] = "Bearer #{ token } "
75
85
configuration . scheme = 'https'
76
86
configuration . host = "#{ host } :#{ port } "
77
87
configuration . ssl_ca_cert = ca_cert
88
+ return unless try_refresh_token
89
+
90
+ Configuration . instance_variable_set ( :@in_cluster_config , self )
91
+ Configuration . prepend ( Module . new do
92
+ # rubocop:disable Metrics/LineLength
93
+ def api_key_with_prefix ( identifier )
94
+ in_cluster_config = self . class . instance_variable_get ( :@in_cluster_config )
95
+ if identifier == 'authorization' && @api_key . key? ( identifier ) && in_cluster_config . token_expires_at <= Time . now
96
+ in_cluster_config . load_token
97
+ @api_key [ identifier ] = 'Bearer ' + in_cluster_config . token
98
+ end
99
+ super identifier
100
+ end
101
+ # rubocop:enable Metrics/LineLength
102
+ end )
78
103
end
104
+ # rubocop:enable Metrics/AbcSize
79
105
end
80
106
end
0 commit comments