|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from typing import Dict, List, Tuple, Optional |
| 4 | +import re |
| 5 | + |
| 6 | +from ..call_wrappers import call, CallVerbosity |
| 7 | +from ..container_daemon_form import ContainerDaemonForm, daemon_to_container |
| 8 | +from ..container_types import CephContainer |
| 9 | +from ..context import CephadmContext |
| 10 | +from ..context_getters import fetch_configs |
| 11 | +from ..daemon_form import register as register_daemon_form |
| 12 | +from ..daemon_identity import DaemonIdentity |
| 13 | +from ..deployment_utils import to_deployment_container |
| 14 | +from ..constants import DEFAULT_OAUTH2_PROXY_IMAGE, UID_NOBODY, GID_NOGROUP |
| 15 | +from ..data_utils import dict_get, is_fsid |
| 16 | +from ..file_utils import populate_files, makedirs, recursive_chown |
| 17 | +from ..exceptions import Error |
| 18 | + |
| 19 | + |
| 20 | +logger = logging.getLogger() |
| 21 | + |
| 22 | + |
| 23 | +@register_daemon_form |
| 24 | +class OAuth2Proxy(ContainerDaemonForm): |
| 25 | + """Define the configs for the jaeger tracing containers""" |
| 26 | + |
| 27 | + default_image = DEFAULT_OAUTH2_PROXY_IMAGE |
| 28 | + daemon_type = 'oauth2-proxy' |
| 29 | + required_files = [ |
| 30 | + 'oauth2-proxy.conf', |
| 31 | + 'oauth2-proxy.crt', |
| 32 | + 'oauth2-proxy.key', |
| 33 | + ] |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def for_daemon_type(cls, daemon_type: str) -> bool: |
| 37 | + return cls.daemon_type == daemon_type |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + ctx: CephadmContext, |
| 42 | + fsid: str, |
| 43 | + daemon_id: str, |
| 44 | + config_json: Dict, |
| 45 | + image: str = DEFAULT_OAUTH2_PROXY_IMAGE, |
| 46 | + ): |
| 47 | + self.ctx = ctx |
| 48 | + self.fsid = fsid |
| 49 | + self.daemon_id = daemon_id |
| 50 | + self.image = image |
| 51 | + self.files = dict_get(config_json, 'files', {}) |
| 52 | + self.validate() |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def init( |
| 56 | + cls, ctx: CephadmContext, fsid: str, daemon_id: str |
| 57 | + ) -> 'OAuth2Proxy': |
| 58 | + return cls(ctx, fsid, daemon_id, fetch_configs(ctx), ctx.image) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def create( |
| 62 | + cls, ctx: CephadmContext, ident: DaemonIdentity |
| 63 | + ) -> 'OAuth2Proxy': |
| 64 | + return cls.init(ctx, ident.fsid, ident.daemon_id) |
| 65 | + |
| 66 | + @property |
| 67 | + def identity(self) -> DaemonIdentity: |
| 68 | + return DaemonIdentity(self.fsid, self.daemon_type, self.daemon_id) |
| 69 | + |
| 70 | + def container(self, ctx: CephadmContext) -> CephContainer: |
| 71 | + ctr = daemon_to_container(ctx, self) |
| 72 | + return to_deployment_container(ctx, ctr) |
| 73 | + |
| 74 | + def uid_gid(self, ctx: CephadmContext) -> Tuple[int, int]: |
| 75 | + return UID_NOBODY, GID_NOGROUP |
| 76 | + |
| 77 | + def get_daemon_args(self) -> List[str]: |
| 78 | + return [ |
| 79 | + '--config=/etc/oauth2-proxy.conf', |
| 80 | + '--tls-cert-file=/etc/oauth2-proxy.crt', |
| 81 | + '--tls-key-file=/etc/oauth2-proxy.key', |
| 82 | + ] |
| 83 | + |
| 84 | + def default_entrypoint(self) -> str: |
| 85 | + return '' |
| 86 | + |
| 87 | + def create_daemon_dirs(self, data_dir: str, uid: int, gid: int) -> None: |
| 88 | + """Create files under the container data dir""" |
| 89 | + if not os.path.isdir(data_dir): |
| 90 | + raise OSError('data_dir is not a directory: %s' % (data_dir)) |
| 91 | + logger.info('Writing oauth2-proxy config...') |
| 92 | + config_dir = os.path.join(data_dir, 'etc/') |
| 93 | + makedirs(config_dir, uid, gid, 0o755) |
| 94 | + recursive_chown(config_dir, uid, gid) |
| 95 | + populate_files(config_dir, self.files, uid, gid) |
| 96 | + |
| 97 | + def validate(self) -> None: |
| 98 | + if not is_fsid(self.fsid): |
| 99 | + raise Error(f'not an fsid: {self.fsid}') |
| 100 | + if not self.daemon_id: |
| 101 | + raise Error(f'invalid daemon_id: {self.daemon_id}') |
| 102 | + if not self.image: |
| 103 | + raise Error(f'invalid image: {self.image}') |
| 104 | + |
| 105 | + # check for the required files |
| 106 | + if self.required_files: |
| 107 | + for fname in self.required_files: |
| 108 | + if fname not in self.files: |
| 109 | + raise Error( |
| 110 | + 'required file missing from config-json: %s' % fname |
| 111 | + ) |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def get_version(ctx: CephadmContext, container_id: str) -> Optional[str]: |
| 115 | + """Return the version of the oauth2-proxy container""" |
| 116 | + version = None |
| 117 | + out, err, code = call( |
| 118 | + ctx, |
| 119 | + [ |
| 120 | + ctx.container_engine.path, |
| 121 | + 'exec', |
| 122 | + container_id, |
| 123 | + 'oauth2-proxy', |
| 124 | + '--version', |
| 125 | + ], |
| 126 | + verbosity=CallVerbosity.QUIET, |
| 127 | + ) |
| 128 | + if code == 0: |
| 129 | + match = re.search(r'oauth2-proxy (v\d+\.\d+\.\d+)', out) |
| 130 | + if match: |
| 131 | + version = match.group(1) |
| 132 | + return version |
| 133 | + |
| 134 | + def customize_container_mounts( |
| 135 | + self, ctx: CephadmContext, mounts: Dict[str, str] |
| 136 | + ) -> None: |
| 137 | + data_dir = self.identity.data_dir(ctx.data_dir) |
| 138 | + mounts.update( |
| 139 | + { |
| 140 | + os.path.join( |
| 141 | + data_dir, 'etc/oauth2-proxy.conf' |
| 142 | + ): '/etc/oauth2-proxy.conf:Z', |
| 143 | + os.path.join( |
| 144 | + data_dir, 'etc/oauth2-proxy.crt' |
| 145 | + ): '/etc/oauth2-proxy.crt:Z', |
| 146 | + os.path.join( |
| 147 | + data_dir, 'etc/oauth2-proxy.key' |
| 148 | + ): '/etc/oauth2-proxy.key:Z', |
| 149 | + } |
| 150 | + ) |
| 151 | + |
| 152 | + def customize_container_args( |
| 153 | + self, ctx: CephadmContext, args: List[str] |
| 154 | + ) -> None: |
| 155 | + uid, _ = self.uid_gid(ctx) |
| 156 | + other_args = [ |
| 157 | + '--user', |
| 158 | + str(uid), |
| 159 | + ] |
| 160 | + args.extend(other_args) |
| 161 | + |
| 162 | + def customize_process_args( |
| 163 | + self, ctx: CephadmContext, args: List[str] |
| 164 | + ) -> None: |
| 165 | + args.extend(self.get_daemon_args()) |
0 commit comments