@@ -7,6 +7,7 @@ use tracing::{debug, trace};
77use tree_sitter:: Node ;
88
99use crate :: configure:: context:: Context ;
10+ use crate :: configure:: parameters:: is_secure_value;
1011use crate :: dscerror:: DscError ;
1112use crate :: functions:: FunctionDispatcher ;
1213use crate :: parser:: functions:: Function ;
@@ -113,17 +114,30 @@ impl Expression {
113114 /// This function will return an error if the expression fails to execute.
114115 pub fn invoke ( & self , function_dispatcher : & FunctionDispatcher , context : & Context ) -> Result < Value , DscError > {
115116 let result = self . function . invoke ( function_dispatcher, context) ?;
116- // skip trace if function is 'secret()'
117- if self . function . name ( ) != "secret" {
117+ if self . function . name ( ) != "secret" && !is_secure_value ( & result) {
118118 let result_json = serde_json:: to_string ( & result) ?;
119119 trace ! ( "{}" , t!( "parser.expression.functionResult" , results = result_json) ) ;
120+ } else {
121+ trace ! ( "{}" , t!( "parser.expression.functionResultSecure" ) ) ;
120122 }
121123 if self . accessors . is_empty ( ) {
122124 Ok ( result)
123125 }
124126 else {
125127 debug ! ( "{}" , t!( "parser.expression.evalAccessors" ) ) ;
126128 let mut value = result;
129+ let is_secure = is_secure_value ( & value) ;
130+ if is_secure {
131+ // if a SecureString, extract the string value
132+ if let Some ( string) = value. get ( "secureString" ) {
133+ if let Some ( s) = string. as_str ( ) {
134+ value = Value :: String ( s. to_string ( ) ) ;
135+ }
136+ } else if let Some ( obj) = value. get ( "secureObject" ) {
137+ // if a SecureObject, extract the object value
138+ value = obj. clone ( ) ;
139+ }
140+ }
127141 for accessor in & self . accessors {
128142 let mut index = Value :: Null ;
129143 match accessor {
@@ -132,7 +146,12 @@ impl Expression {
132146 if !object. contains_key ( member) {
133147 return Err ( DscError :: Parser ( t ! ( "parser.expression.memberNameNotFound" , member = member) . to_string ( ) ) ) ;
134148 }
135- value = object[ member] . clone ( ) ;
149+ if is_secure {
150+ // if the original value was a secure value, we need to convert the member value back to secure
151+ value = convert_to_secure ( & object[ member] ) ;
152+ } else {
153+ value = object[ member] . clone ( ) ;
154+ }
136155 } else {
137156 return Err ( DscError :: Parser ( t ! ( "parser.expression.accessOnNonObject" ) . to_string ( ) ) ) ;
138157 }
@@ -169,3 +188,27 @@ impl Expression {
169188 }
170189 }
171190}
191+
192+ fn convert_to_secure ( value : & Value ) -> Value {
193+ if let Some ( string) = value. as_str ( ) {
194+ let secure_string = crate :: configure:: parameters:: SecureString {
195+ secure_string : string. to_string ( ) ,
196+ } ;
197+ return serde_json:: to_value ( secure_string) . unwrap_or ( value. clone ( ) ) ;
198+ }
199+
200+ if let Some ( obj) = value. as_object ( ) {
201+ if obj. len ( ) == 1 && obj. contains_key ( "secureObject" ) {
202+ let secure_object = crate :: configure:: parameters:: SecureObject {
203+ secure_object : obj[ "secureObject" ] . clone ( ) ,
204+ } ;
205+ return serde_json:: to_value ( secure_object) . unwrap_or ( value. clone ( ) ) ;
206+ }
207+ }
208+
209+ if let Some ( array) = value. as_array ( ) {
210+ let new_array: Vec < Value > = array. iter ( ) . map ( convert_to_secure) . collect ( ) ;
211+ return Value :: Array ( new_array) ;
212+ }
213+ value. clone ( )
214+ }
0 commit comments