2121- Appropriate permissions to manage organization tokens
2222"""
2323
24- import os
25- import sys
2624from datetime import datetime , timedelta
2725
2826# Add the src directory to the path
29- sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), ".." , "src" ))
30-
27+ ##sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
3128from pytfe import TFEClient , TFEConfig
3229from pytfe .models import (
3330 OrganizationTokenCreateOptions ,
3734)
3835
3936
37+ def redact_token (token_value : str | None ) -> str :
38+ """Redact token value for safe display."""
39+ if not token_value :
40+ return "None"
41+ if len (token_value ) <= 8 :
42+ return f"{ '*' * len (token_value )} "
43+ # Show first 3 and last 3 characters
44+ return f"{ token_value [:3 ]} ...{ token_value [- 3 :]} " .replace (
45+ token_value [3 :- 3 ], "*" * (len (token_value ) - 6 )
46+ )
47+
48+
49+ def redact_id (id_value : str | None ) -> str :
50+ """Redact ID for safe display."""
51+ if not id_value :
52+ return "None"
53+ if len (id_value ) <= 6 :
54+ return f"{ '*' * len (id_value )} "
55+ # Show first 3 and last 3 characters
56+ return f"{ id_value [:3 ]} ...{ id_value [- 3 :]} "
57+
58+
4059def main ():
4160 """Execute organization token operation examples."""
4261
@@ -46,8 +65,7 @@ def main():
4665
4766 # Initialize the TFE client
4867 client = TFEClient (TFEConfig .from_env ())
49- organization_name = "my-org" # Change to your organization name
50-
68+ organization_name = "prab-sandbox01"
5169 # =====================================================
5270 # 1. CREATE ORGANIZATION TOKEN (BASIC)
5371 # =====================================================
@@ -58,16 +76,16 @@ def main():
5876 token = client .organization_tokens .create (organization_name )
5977
6078 print ("Token created successfully!" )
61- print (f" Token ID: { token .id } " )
79+ print (f" Token ID: { redact_id ( token .id ) } " )
6280 print (f" Created At: { token .created_at } " )
6381 print (f" Description: { token .description } " )
64- print (f" Token Value: { token .token } " )
82+ print (f" Token Value: { redact_token ( token .token ) } " )
6583 if token .expired_at :
6684 print (f" Expires At: { token .expired_at } " )
6785 print ()
6886
6987 except Exception as e :
70- print (f"✗ Error: { e } " )
88+ print (f" Error: { e } " )
7189 print ()
7290
7391 # =====================================================
@@ -86,7 +104,7 @@ def main():
86104 )
87105
88106 print ("Token created with options successfully!" )
89- print (f" Token ID: { token .id } " )
107+ print (f" Token ID: { redact_id ( token .id ) } " )
90108 print (f" Created At: { token .created_at } " )
91109 if token .expired_at :
92110 print (f" Expires At: { token .expired_at } " )
@@ -108,12 +126,12 @@ def main():
108126 )
109127
110128 print (" Audit-trails token created successfully!" )
111- print (f" Token ID: { token .id } " )
112- print (f" Token Value: { token .token } " )
129+ print (f" Token ID: { redact_id ( token .id ) } " )
130+ print (f" Token Value: { redact_token ( token .token ) } " )
113131 print ()
114132
115133 except Exception as e :
116- print (f"✗ Error: { e } " )
134+ print (f"Error: { e } " )
117135 print ()
118136
119137 # =====================================================
@@ -124,7 +142,7 @@ def main():
124142 token = client .organization_tokens .read (organization_name )
125143
126144 print ("Token read successfully!" )
127- print (f" Token ID: { token .id } " )
145+ print (f" Token ID: { redact_id ( token .id ) } " )
128146 print (f" Created At: { token .created_at } " )
129147 print (f" Description: { token .description } " )
130148 if token .last_used_at :
@@ -134,7 +152,7 @@ def main():
134152 print ()
135153
136154 except Exception as e :
137- print (f"✗ Error: { e } " )
155+ print (f" Error: { e } " )
138156 print ()
139157
140158 # =====================================================
@@ -147,12 +165,12 @@ def main():
147165 token = client .organization_tokens .read_with_options (organization_name , options )
148166
149167 print (" Audit-trails token read successfully!" )
150- print (f" Token ID: { token .id } " )
151- print (f" Token Value: { token .token } " )
168+ print (f" Token ID: { redact_id ( token .id ) } " )
169+ print (f" Token Value: { redact_token ( token .token ) } " )
152170 print ()
153171
154172 except Exception as e :
155- print (f"✗ Error: { e } " )
173+ print (f" Error: { e } " )
156174 print ()
157175
158176 # =====================================================
0 commit comments