21
21
22
22
public class AbstractManager extends LoggingObject {
23
23
24
- protected PayloadParser payloadParser = new PayloadParser ();
24
+ protected PayloadParser payloadParser = new PayloadParser ();
25
25
26
- /**
27
- * Manager classes that need to connect to ML as a user with the manage-admin and security roles (e.g. all the
28
- * classes for Security resources) should override this to return true.
29
- *
30
- * The main use case for this is while an application may define a user with the manage-admin role that can be used
31
- * for deploying most resources, that user must first be created. And thus, some user with at least the manage-admin
32
- * and security roles must already exist and must be used to create that user.
33
- *
34
- * @return
35
- */
36
- protected boolean useSecurityUser () {
37
- return false ;
38
- }
26
+ /**
27
+ * Manager classes that need to connect to ML as a user with the manage-admin and security roles (e.g. all the
28
+ * classes for Security resources) should override this to return true.
29
+ * <p>
30
+ * The main use case for this is while an application may define a user with the manage-admin role that can be used
31
+ * for deploying most resources, that user must first be created. And thus, some user with at least the manage-admin
32
+ * and security roles must already exist and must be used to create that user.
33
+ *
34
+ * @return
35
+ */
36
+ protected boolean useSecurityUser () {
37
+ return false ;
38
+ }
39
39
40
40
/**
41
41
* Some payloads - such as a server payload that uses external security - require a condition to determine if the
@@ -45,57 +45,76 @@ protected boolean useSecurityUser() {
45
45
* @return
46
46
*/
47
47
protected boolean useSecurityUser (String payload ) {
48
- return useSecurityUser ();
49
- }
48
+ return useSecurityUser ();
49
+ }
50
+
51
+ /**
52
+ * Assumes the resource name is based on the class name - e.g. RoleManager would have a resource name of "role".
53
+ *
54
+ * @return
55
+ */
56
+ protected String getResourceName () {
57
+ String name = ClassUtils .getShortName (getClass ());
58
+ name = name .replace ("Manager" , "" );
59
+ return name .toLowerCase ();
60
+ }
61
+
62
+ /**
63
+ * Assumes the field name of the resource ID - which is used to determine existence - is the resource name plus
64
+ * "-name". So RoleManager would have an ID field name of "role-name".
65
+ *
66
+ * @return
67
+ */
68
+ protected String getIdFieldName () {
69
+ return getResourceName () + "-name" ;
70
+ }
50
71
51
- /**
52
- * Assumes the resource name is based on the class name - e.g. RoleManager would have a resource name of "role".
53
- *
54
- * @return
55
- */
56
- protected String getResourceName () {
57
- String name = ClassUtils .getShortName (getClass ());
58
- name = name .replace ("Manager" , "" );
59
- return name .toLowerCase ();
60
- }
72
+ protected String getResourceId (String payload ) {
73
+ return payloadParser .getPayloadFieldValue (payload , getIdFieldName ());
74
+ }
61
75
62
- /**
63
- * Assumes the field name of the resource ID - which is used to determine existence - is the resource name plus
64
- * "-name". So RoleManager would have an ID field name of "role-name".
65
- *
66
- * @return
67
- */
68
- protected String getIdFieldName () {
69
- return getResourceName () + "-name" ;
70
- }
76
+ protected ResponseEntity <String > putPayload (ManageClient client , String path , String payload ) {
77
+ boolean requiresSecurityUser = useSecurityUser (payload );
78
+ try {
79
+ if (payloadParser .isJsonPayload (payload )) {
80
+ return requiresSecurityUser ? client .putJsonAsSecurityUser (path , payload ) : client .putJson (path , payload );
81
+ }
82
+ return requiresSecurityUser ? client .putXmlAsSecurityUser (path , payload ) : client .putXml (path , payload );
83
+ } catch (RuntimeException ex ) {
84
+ logRequestBodyToAssistWithDebugging ("PUT" , path , payload );
85
+ throw ex ;
86
+ }
87
+ }
71
88
72
- protected String getResourceId (String payload ) {
73
- return payloadParser .getPayloadFieldValue (payload , getIdFieldName ());
74
- }
89
+ protected ResponseEntity <String > postPayload (ManageClient client , String path , String payload ) {
90
+ boolean requiresSecurityUser = useSecurityUser (payload );
91
+ try {
92
+ if (payloadParser .isJsonPayload (payload )) {
93
+ return requiresSecurityUser ? client .postJsonAsSecurityUser (path , payload ) : client .postJson (path , payload );
94
+ }
95
+ return requiresSecurityUser ? client .postXmlAsSecurityUser (path , payload ) : client .postXml (path , payload );
96
+ } catch (RuntimeException ex ) {
97
+ logRequestBodyToAssistWithDebugging ("POST" , path , payload );
98
+ throw ex ;
99
+ }
100
+ }
75
101
76
- protected ResponseEntity <String > putPayload (ManageClient client , String path , String payload ) {
77
- boolean requiresSecurityUser = useSecurityUser (payload );
78
- try {
79
- if (payloadParser .isJsonPayload (payload )) {
80
- return requiresSecurityUser ? client .putJsonAsSecurityUser (path , payload ) : client .putJson (path , payload );
81
- }
82
- return requiresSecurityUser ? client .putXmlAsSecurityUser (path , payload ) : client .putXml (path , payload );
83
- } catch (RuntimeException ex ) {
84
- logger .error (format ("Error occurred while sending PUT request to %s; logging request body to assist with debugging: %s" , path , payload ));
85
- throw ex ;
86
- }
87
- }
102
+ protected void logRequestBodyToAssistWithDebugging (String httpMethod , String path , String payload ) {
103
+ if (!payloadContainsSensitiveValues (payload )) {
104
+ logger .error (format ("Error occurred while sending %s request to %s; not logging request body to avoid leaking sensitive values." ,
105
+ httpMethod , path ));
106
+ } else {
107
+ logger .error (format ("Error occurred while sending %s request to %s; logging request body to assist with debugging: %s" ,
108
+ httpMethod , path , payload ));
109
+ }
110
+ }
88
111
89
- protected ResponseEntity <String > postPayload (ManageClient client , String path , String payload ) {
90
- boolean requiresSecurityUser = useSecurityUser (payload );
91
- try {
92
- if (payloadParser .isJsonPayload (payload )) {
93
- return requiresSecurityUser ? client .postJsonAsSecurityUser (path , payload ) : client .postJson (path , payload );
94
- }
95
- return requiresSecurityUser ? client .postXmlAsSecurityUser (path , payload ) : client .postXml (path , payload );
96
- } catch (RuntimeException ex ) {
97
- logger .error (format ("Error occurred while sending POST request to %s; logging request body to assist with debugging: %s" , path , payload ));
98
- throw ex ;
99
- }
100
- }
112
+ protected boolean payloadContainsSensitiveValues (String payload ) {
113
+ // pkey is a common field name for private keys in requests to the MarkLogic Manage API.
114
+ return payload != null && (payload .contains ("\" pkey\" " ) ||
115
+ payload .contains ("PRIVATE KEY" ) ||
116
+ payload .contains ("\" password\" " ) ||
117
+ payload .contains ("\" passphrase\" " )
118
+ );
119
+ }
101
120
}
0 commit comments