Skip to content

Commit 24d2a81

Browse files
committed
Add logging table
Removals and updates to certificates are logged in table `/apmg/strust_log`. Currently, only successful runs are saved. In case of errors, no log will be written. When application logging will be added, we will also log errors.
1 parent ee4c024 commit 24d2a81

8 files changed

+329
-31
lines changed

src/#apmg#cl_strust.clas.abap

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CLASS /apmg/cl_strust DEFINITION
1313
************************************************************************
1414
PUBLIC SECTION.
1515

16-
CONSTANTS c_version TYPE string VALUE '2.0.2' ##NEEDED.
16+
CONSTANTS c_version TYPE string VALUE '2.1.0' ##NEEDED.
1717

1818
CONSTANTS:
1919
BEGIN OF c_context ##NEEDED,
@@ -113,14 +113,17 @@ CLASS /apmg/cl_strust DEFINITION
113113
METHODS remove
114114
IMPORTING
115115
!subject TYPE string
116+
!comment TYPE string OPTIONAL
116117
RETURNING
117118
VALUE(result) TYPE REF TO /apmg/cl_strust
118119
RAISING
119120
/apmg/cx_error.
120121

121122
METHODS update
122123
IMPORTING
124+
!comment TYPE string OPTIONAL
123125
!remove_expired TYPE abap_bool DEFAULT abap_false
126+
PREFERRED PARAMETER comment
124127
RETURNING
125128
VALUE(result) TYPE ty_certattr_tt
126129
RAISING
@@ -143,6 +146,7 @@ CLASS /apmg/cl_strust DEFINITION
143146
certs_new TYPE ty_certattr_tt,
144147
cert_current TYPE ty_certattr,
145148
certs_current TYPE ty_certattr_tt,
149+
logs TYPE STANDARD TABLE OF /apmg/strust_log WITH KEY timestamp counter,
146150
is_dirty TYPE abap_bool.
147151

148152
METHODS _create
@@ -168,6 +172,23 @@ CLASS /apmg/cl_strust DEFINITION
168172
RAISING
169173
/apmg/cx_error.
170174

175+
METHODS _log_add
176+
IMPORTING
177+
!subject TYPE ty_certattr-subject
178+
!issuer TYPE ty_certattr-issuer
179+
!date_from TYPE ty_certattr-date_from
180+
!date_to TYPE ty_certattr-date_to
181+
!status TYPE /apmg/strust_log-status
182+
!message TYPE /apmg/strust_log-message
183+
RAISING
184+
/apmg/cx_error.
185+
186+
METHODS _log_save
187+
IMPORTING
188+
!comment TYPE string
189+
RAISING
190+
/apmg/cx_error.
191+
171192
ENDCLASS.
172193

173194

@@ -434,7 +455,7 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
434455

435456
_profile( ).
436457

437-
" Remove certificate
458+
" Remove certificates
438459
LOOP AT certs_current ASSIGNING FIELD-SYMBOL(<cert>) WHERE subject = subject.
439460

440461
CALL FUNCTION 'SSFC_REMOVECERTIFICATE'
@@ -456,13 +477,21 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
456477
RAISE EXCEPTION TYPE /apmg/cx_error_t100.
457478
ENDIF.
458479

480+
_log_add(
481+
subject = <cert>-subject
482+
issuer = <cert>-issuer
483+
date_from = <cert>-date_from
484+
date_to = <cert>-date_to
485+
status = icon_led_green
486+
message = 'Removed' ).
487+
459488
is_dirty = abap_true.
460489

461490
ENDLOOP.
462491

463492
_save( ).
464493

465-
_unlock( ).
494+
_log_save( comment ).
466495

467496
result = me.
468497

@@ -501,6 +530,14 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
501530
RAISE EXCEPTION TYPE /apmg/cx_error_t100.
502531
ENDIF.
503532

533+
_log_add(
534+
subject = <cert>-subject
535+
issuer = <cert>-issuer
536+
date_from = <cert>-date_from
537+
date_to = <cert>-date_to
538+
status = icon_led_green
539+
message = 'Removed' ).
540+
504541
is_dirty = abap_true.
505542
ELSE.
506543
" Certificate already exists, no update necessary
@@ -512,7 +549,7 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
512549
ENDLOOP.
513550
ENDIF.
514551

515-
" Add new certificates to PSE
552+
" Add new certificates
516553
LOOP AT certs_new ASSIGNING <cert_new>.
517554

518555
CALL FUNCTION 'SSFC_PUT_CERTIFICATE'
@@ -539,12 +576,21 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
539576
RAISE EXCEPTION TYPE /apmg/cx_error_t100.
540577
ENDIF.
541578

579+
_log_add(
580+
subject = <cert_new>-subject
581+
issuer = <cert_new>-issuer
582+
date_from = <cert_new>-date_from
583+
date_to = <cert_new>-date_to
584+
status = icon_led_green
585+
message = 'Added' ).
586+
542587
is_dirty = abap_true.
588+
543589
ENDLOOP.
544590

545591
_save( ).
546592

547-
_unlock( ).
593+
_log_save( comment ).
548594

549595
result = certs_new.
550596

@@ -621,6 +667,40 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
621667
ENDMETHOD.
622668

623669

670+
METHOD _log_add.
671+
672+
DATA(log) = VALUE /apmg/strust_log(
673+
cert_subject = subject
674+
cert_issues = issuer
675+
date_from = date_from
676+
date_to = date_to
677+
status = status
678+
message = message ).
679+
680+
INSERT log INTO TABLE logs.
681+
682+
ENDMETHOD.
683+
684+
685+
METHOD _log_save.
686+
687+
GET TIME STAMP FIELD DATA(timestamp).
688+
689+
LOOP AT logs ASSIGNING FIELD-SYMBOL(<log>).
690+
<log>-timestamp = timestamp.
691+
<log>-counter = sy-tabix.
692+
<log>-username = sy-uname.
693+
<log>-comments = comment.
694+
ENDLOOP.
695+
696+
INSERT /apmg/strust_log FROM TABLE logs.
697+
IF sy-subrc <> 0.
698+
RAISE EXCEPTION TYPE /apmg/cx_error_text EXPORTING text = 'Error saving comments to log table'(012).
699+
ENDIF.
700+
701+
ENDMETHOD.
702+
703+
624704
METHOD _profile.
625705

626706
IF tempfile IS NOT INITIAL.
@@ -680,6 +760,8 @@ CLASS /apmg/cl_strust IMPLEMENTATION.
680760
MESSAGE 'Certificate was saved successfully' TYPE 'S'.
681761
ENDIF.
682762

763+
_unlock( ).
764+
683765
ENDMETHOD.
684766

685767

src/#apmg#cl_strust.clas.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
<ENTRY>Missing profile. Call &quot;load&quot; first</ENTRY>
2525
<LENGTH>80</LENGTH>
2626
</item>
27+
<item>
28+
<ID>I</ID>
29+
<KEY>012</KEY>
30+
<ENTRY>Error saving comments to log table</ENTRY>
31+
<LENGTH>80</LENGTH>
32+
</item>
2733
<item>
2834
<ID>I</ID>
2935
<KEY>020</KEY>

src/#apmg#cl_strust_cert_api.clas.abap

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ CLASS /apmg/cl_strust_cert_api DEFINITION
1414
************************************************************************
1515
PUBLIC SECTION.
1616

17+
CONSTANTS:
18+
c_api_host TYPE string VALUE 'https://tools.abappm.com',
19+
c_api_endpoint TYPE string VALUE '/api/v1/certificates'.
20+
1721
CLASS-METHODS get_certificates
1822
IMPORTING
1923
!domain TYPE string
2024
!ssl_id TYPE ssfapplssl DEFAULT 'ANONYM'
2125
!debug TYPE abap_bool DEFAULT abap_false
26+
!host TYPE string DEFAULT c_api_host
27+
!endpoint TYPE string DEFAULT c_api_endpoint
2228
RETURNING
2329
VALUE(result) TYPE string
2430
RAISING
@@ -27,13 +33,10 @@ CLASS /apmg/cl_strust_cert_api DEFINITION
2733
PROTECTED SECTION.
2834
PRIVATE SECTION.
2935

30-
CONSTANTS:
31-
c_api_host TYPE string VALUE 'https://tools.abappm.com',
32-
c_api_endpoint TYPE string VALUE '/api/v1/certificates'.
33-
3436
CLASS-METHODS _client
3537
IMPORTING
3638
ssl_id TYPE ssfapplssl
39+
host TYPE string
3740
uri TYPE string
3841
RETURNING
3942
VALUE(result) TYPE REF TO if_http_client
@@ -71,7 +74,8 @@ CLASS /apmg/cl_strust_cert_api IMPLEMENTATION.
7174

7275
DATA(http_client) = _client(
7376
ssl_id = ssl_id
74-
uri = |{ c_api_endpoint }?domain={ query }| ).
77+
host = host
78+
uri = |{ endpoint }?domain={ query }| ).
7579

7680
DATA(fetch_response) = _response( http_client ).
7781

@@ -98,7 +102,7 @@ CLASS /apmg/cl_strust_cert_api IMPLEMENTATION.
98102

99103
cl_http_client=>create_by_url(
100104
EXPORTING
101-
url = c_api_host
105+
url = host
102106
ssl_id = ssl_id
103107
IMPORTING
104108
client = result

src/#apmg#strust_installer.prog.abap

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ SELECTION-SCREEN END OF BLOCK b1.
1515

1616
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-t02.
1717
PARAMETERS p_domain TYPE string OBLIGATORY LOWER CASE.
18+
SELECTION-SCREEN SKIP.
19+
PARAMETERS p_text TYPE string LOWER CASE.
1820
SELECTION-SCREEN END OF BLOCK b2.
1921

22+
SELECTION-SCREEN BEGIN OF BLOCK b4 WITH FRAME TITLE TEXT-t04.
23+
PARAMETERS:
24+
p_ssl_id TYPE ssfappl DEFAULT 'ANONYM',
25+
p_host TYPE string OBLIGATORY LOWER CASE,
26+
p_endpnt TYPE string OBLIGATORY LOWER CASE.
27+
SELECTION-SCREEN END OF BLOCK b4.
28+
2029
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-t03.
2130
PARAMETERS:
2231
p_passwd TYPE string LOWER CASE,
@@ -31,12 +40,20 @@ INITIALIZATION.
3140
+ cl_abap_pse=>authority_check( iv_activity = '02' )
3241
+ cl_abap_pse=>authority_check( iv_activity = '06' ).
3342
IF subrc <> 0.
34-
MESSAGE 'You are not authorized to update certificates' TYPE 'E'.
43+
MESSAGE 'You are not authorized to install certificates' TYPE 'I' DISPLAY LIKE 'E'.
3544
STOP.
3645
ENDIF.
3746

47+
p_host = /apmg/cl_strust_cert_api=>c_api_host.
48+
p_endpnt = /apmg/cl_strust_cert_api=>c_api_endpoint.
49+
3850
START-OF-SELECTION.
3951

52+
IF p_root IS INITIAL AND p_main IS INITIAL.
53+
MESSAGE 'No certificates selected for installation' TYPE 'I' DISPLAY LIKE 'E'.
54+
STOP.
55+
ENDIF.
56+
4057
CALL FUNCTION 'SSFPSE_PARAMETER'
4158
EXPORTING
4259
context = p_cont
@@ -45,25 +62,29 @@ START-OF-SELECTION.
4562
pse_not_found = 1
4663
OTHERS = 2.
4764
IF sy-subrc <> 0.
48-
MESSAGE 'PSE not found' TYPE 'E'.
65+
MESSAGE 'PSE not found' TYPE 'I' DISPLAY LIKE 'E'.
4966
STOP.
5067
ENDIF.
5168

5269
TRY.
5370
DATA(strust) = /apmg/cl_strust=>create(
5471
context = p_cont
5572
application = p_appl
56-
password = p_passwd ).
73+
password = p_passwd )->load( ).
5774
CATCH /apmg/cx_error INTO DATA(error).
58-
MESSAGE error TYPE 'E'.
75+
MESSAGE error TYPE 'I' DISPLAY LIKE 'E'.
5976
STOP.
6077
ENDTRY.
6178

6279
WRITE: /'Domain:', p_domain COLOR COL_POSITIVE.
6380
SKIP.
6481

6582
TRY.
66-
DATA(json) = /apmg/cl_strust_cert_api=>get_certificates( p_domain ).
83+
DATA(json) = /apmg/cl_strust_cert_api=>get_certificates(
84+
ssl_id = p_ssl_id
85+
domain = p_domain
86+
host = p_host
87+
endpoint = p_endpnt ).
6788

6889
TRY.
6990
DATA(ajson) = zcl_ajson=>parse( json ).
@@ -150,11 +171,7 @@ START-OF-SELECTION.
150171
WRITE: / 'Test run' COLOR COL_TOTAL, '(changes were not saved)'.
151172
ELSE.
152173

153-
" Load and lock
154-
strust->load( ).
155-
156-
" Save changes
157-
strust->update( ).
174+
strust->update( comment = p_text ).
158175

159176
WRITE / 'Certificates saved' COLOR COL_POSITIVE.
160177

src/#apmg#strust_installer.prog.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<ENTRY>Options</ENTRY>
2929
<LENGTH>17</LENGTH>
3030
</item>
31+
<item>
32+
<ID>I</ID>
33+
<KEY>T04</KEY>
34+
<ENTRY>API</ENTRY>
35+
<LENGTH>80</LENGTH>
36+
</item>
3137
<item>
3238
<ID>R</ID>
3339
<ENTRY>Trust Management: Certificate Installer</ENTRY>
@@ -51,6 +57,18 @@
5157
<ENTRY>Domain</ENTRY>
5258
<LENGTH>14</LENGTH>
5359
</item>
60+
<item>
61+
<ID>S</ID>
62+
<KEY>P_ENDPNT</KEY>
63+
<ENTRY>Endpoint</ENTRY>
64+
<LENGTH>16</LENGTH>
65+
</item>
66+
<item>
67+
<ID>S</ID>
68+
<KEY>P_HOST</KEY>
69+
<ENTRY>Host</ENTRY>
70+
<LENGTH>12</LENGTH>
71+
</item>
5472
<item>
5573
<ID>S</ID>
5674
<KEY>P_MAIN</KEY>
@@ -69,12 +87,24 @@
6987
<ENTRY>Add root/intermediate certs</ENTRY>
7088
<LENGTH>35</LENGTH>
7189
</item>
90+
<item>
91+
<ID>S</ID>
92+
<KEY>P_SSL_ID</KEY>
93+
<ENTRY>SSL ID</ENTRY>
94+
<LENGTH>14</LENGTH>
95+
</item>
7296
<item>
7397
<ID>S</ID>
7498
<KEY>P_TEST</KEY>
7599
<ENTRY>Test run</ENTRY>
76100
<LENGTH>16</LENGTH>
77101
</item>
102+
<item>
103+
<ID>S</ID>
104+
<KEY>P_TEXT</KEY>
105+
<ENTRY>Description of change</ENTRY>
106+
<LENGTH>29</LENGTH>
107+
</item>
78108
</TPOOL>
79109
</asx:values>
80110
</asx:abap>

0 commit comments

Comments
 (0)