[필수 과제] openstack token issue CLI 사용법 정리 하기
openstack toiken issue
새로운 인증 정보를 입력받아 토큰을 생성하는 것이 아니라, 현재 인증된 (로그인된) 세션의 토큰 정보를 다시 조회하여 보여주는 역할을 합니다.
즉, 이미 발급받은 토큰의 ID, 만료 시간, 스코프(프로젝트, 도메인 등)와 같은 상세 정보를 확인하는 데 사용됩니다.
# openstackclient/identity/v3/token.py
class IssueToken(command.ShowOne):
_description = _("Issue new token")
# scoped token is optional
required_scope = False
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
return parser
def take_action(self, parsed_args):
auth_ref = self.app.client_manager.auth_ref
if not auth_ref:
raise exceptions.AuthorizationFailure(
_("Only an authorized user may issue a new token.")
)
data = {}
if auth_ref.auth_token:
data['id'] = auth_ref.auth_token
if auth_ref.expires:
datetime_obj = auth_ref.expires
expires_str = datetime_obj.strftime('%Y-%m-%dT%H:%M:%S%z')
data['expires'] = expires_str
if auth_ref.project_id:
data['project_id'] = auth_ref.project_id
if auth_ref.user_id:
data['user_id'] = auth_ref.user_id
if auth_ref.domain_id:
data['domain_id'] = auth_ref.domain_id
if auth_ref.system_scoped:
# NOTE(lbragstad): This could change in the future when, or if,
# keystone supports the ability to scope to a subset of the entire
# deployment system. When that happens, this will have to relay
# scope information and IDs like we do for projects and domains.
data['system'] = 'all'
return zip(*sorted(data.items()))
우선 실제로 openstack token issue 명령어 사용법을 분석하기 이전에 openstackclient 코드를 분석하였습니다.
IssueToken클래스
- IssueToken 클래스는
command.ShowOne을 상속받습니다. openstackclient라이브러리에서 ShowOne은 단일 객체의 정보를 Key-Value 형태의 테이블로 출력하는 데 사용되는 기반 클래스입니다.
$ openstack token issue
+------------+------------------------------------------------------------------+
| Field | Value |
+------------+------------------------------------------------------------------+
| domain_id | default |
| expires | 2025-08-08T09:04:05+0000 |
| id | gAAAAABm3a... |
| project_id | 2f83de4f5f5c439999321e244b4c4e7f |
| user_id | 8a9a6b8b3e4c4e5a8f6d7e8f9a0b1c2d |
+------------+------------------------------------------------------------------+
이러한 상속 구조에 의해 openstack token issue의 결과가 위처럼 2열 테이블 형식으로 나타납니다.
get_parser()
- 해당 메소드는
super()를 호출하기만 하고 추가적인 인자를 정의하지 않습니다. - 이는
openstack token issue명령어가 자체적인 옵션을 가지지 않고, OpenStack 클라이언트의 전역 인증 옵션(OS_PROJECT_ID환경변수 등)에 의존한다는 것을 의미합니다.
take_action()
a. 인증 정보 로드:self.app.client_manager.auth_ref를 통해 현재 클라이언트 세션의 인증 정보 객체인auth_ref를 가져옵니다.- 이 객체에는 이미 인증을 완료하며 얻은 토큰, 사용자 ID, 프로젝트 ID 등의 모든 정보가 담겨 있습니다.
- 만약 로그인되지 않았다면 즉, 객체가 존재하지 않는다면
AuthorizationFailure예외를 발생시킵니다.
b. 데이터 추출 및 구성: auth_ref 객체에서 필요한 정보들을 추출하여 data 딕셔너리에 저장합니다.
c. 출력 데이터 반환:
return zip(*sorted(data.items()))data.items(): (key, value) 튜플의 리스트를 만듭니다.sorted(): 키(Field)를 기준으로 알파벳순으로 정렬합니다.- 이 두 튜플을 반환하면
ShowOne기반 클래스가 이를 받아 테이블 형식으로 출력합니다.
ubuntu@msy-devstack:~$ openstack --version
openstack 7.5.0
ubuntu@msy-devstack:~$ openstack token issue --help
usage: openstack token issue [-h] [-f {json,shell,table,value,yaml}] [-c COLUMN] [--noindent]
[--prefix PREFIX] [--max-width <integer>] [--fit-width]
[--print-empty]
Issue new token
options:
-h, --help show this help message and exit
output formatters:
output formatter options
-f {json,shell,table,value,yaml}, --format {json,shell,table,value,yaml}
the output format, defaults to table
-c COLUMN, --column COLUMN
specify the column(s) to include, can be repeated to show multiple columns
json formatter:
--noindent whether to disable indenting the JSON
shell formatter:
a format a UNIX shell can parse (variable="value")
--prefix PREFIX
add a prefix to all variable names
table formatter:
--max-width <integer>
Maximum display width, <1 to disable. You can also use the CLIFF_MAX_TERM_WIDTH
environment variable, but the parameter takes precedence.
--fit-width Fit the table to the display width. Implied if --max-width greater than 0. Set the
environment variable CLIFF_FIT_WIDTH=1 to always enable
--print-empty Print empty table if there is no data to show.
이후 위와 같이 NHN의 DevStack이 설치된 인스턴스에서 openstack 7.5.0 버전의 openstack token issue 명령어의 사용법을 출력하였습니다.
openstack token issue [option]
일반 옵션
-h, --help: 도움말 메시지를 보여주고 종료합니다.
출력 형식 옵션
-f, --format {json|shell|table|value|yaml}: 결과의 출력 형식을 지정합니다. (기본값:table)-c, --column <COLUMN>: 출력 결과에서 보고 싶은 특정 column만 지정합니다.
JSON 형식 옵션
--noindent: JSON 출력 시 자동으로 적용되는 들여쓰기를 비활성화하여 한 줄로 출력합니다.
Shell 형식 옵션
--prefix <PREFIX>:변수명="값"형태로 출력할 때, 모든 변수명 앞에 지정한 접두사를 붙입니다.
Table 형식 옵션
--max-width <INTEGER>: 테이블의 최대 너비를 지정합니다.--fit-width: 현재 터미널 창의 너비에 맞춰 테이블 크기를 자동으로 조절합니다.--print-empty: 표시할 데이터가 없을 경우, 빈 테이블의 틀이라도 출력합니다.
명령어의 옵션과 각 파라미터는 위와 같이 정리할 수 있었습니다.