summaryrefslogtreecommitdiff
path: root/email-key-rotation/state.scm
blob: 26d7fbe452c74a27641351714609ad3d91a71a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(define-module (email-key-rotation state)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:re-export (make)
  #:export (<email-key-rotation-state>
	    private-key-file
	    selectors
	    current-dkim-selector
	    current-dkim-private-key
	    expired-dkim-private-key
	    set-private-key-file
	    set-selectors
	    set-current-dkim-selector
	    set-current-dkim-private-key
	    set-expired-dkim-private-key
	    the-email-key-rotation-state)
  #:declarative? #t
  #:duplicates (merge-generics))

(define-class <email-key-rotation-state> ()
  (private-key-file #:init-keyword #:private-key-file)
  (selectors #:init-keyword #:selectors)
  (current-dkim-selector #:init-keyword #:current-dkim-selector)
  (current-dkim-private-key #:init-keyword #:current-dkim-private-key)
  (expired-dkim-private-key #:init-keyword #:expired-dkim-private-key
			    #:init-value #f))

(define-method (private-key-file (s <email-key-rotation-state>))
  (slot-ref s 'private-key-file))

(define-method (selectors (s <email-key-rotation-state>))
  (slot-ref s 'selectors))

(define-method (current-dkim-selector (s <email-key-rotation-state>))
  (slot-ref s 'current-dkim-selector))

(define-method (current-dkim-private-key (s <email-key-rotation-state>))
  (slot-ref s 'current-dkim-private-key))

(define-method (expired-dkim-private-key (s <email-key-rotation-state>))
  (slot-ref s 'expired-dkim-private-key))

(define-method (set-private-key-file (s <email-key-rotation-state>) filename)
  (let ((ret (shallow-clone s)))
    (slot-set! ret 'private-key-file filename)
    ret))

(define-method (set-selectors (s <email-key-rotation-state>) selectors)
  (let ((ret (shallow-clone s)))
    (slot-set! ret 'selectors selectors)
    ret))

(define-method (set-current-dkim-selector (s <email-key-rotation-state>) selector)
  (let ((ret (shallow-clone s)))
    (slot-set! ret 'current-dkim-selector selector)
    ret))

(define-method (set-expired-dkim-private-key (s <email-key-rotation-state>) private-key)
  (let ((ret (shallow-clone s)))
    (slot-set! ret 'expired-dkim-private-key private-key)
    ret))

(define-method (the-email-key-rotation-state (s <email-key-rotation-state>))
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-origin 'the-email-key-rotation-state)
	  (make-exception-with-irritants (list s))
	  (make-exception-with-message
	   (format #f "the email key rotation state is invalid."))
	  exn)))
    (lambda ()
      (unless (slot-bound? s 'private-key-file)
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-message
	   (format #f "the private key file name is not bound.")))))
      (unless (string? (private-key-file s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (private-key-file s)))
	  (make-exception-with-message
	   (format #f "the private key file name is not a string.")))))
      (unless (list? (selectors s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (selectors s)))
	  (make-exception-with-message
	   (format #f "the collection of selectors is not a list.")))))
      (when (null? (selectors s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-message
	   (format #f "the list of selectors is empty.")))))
      (when (null? (cdr (selectors s)))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-message
	   (format #f "the list of selectors has only 1 item, cannot rotate anything with that.")))))
      (let check ((sel (selectors s))
		  (i 0))
	(match sel
	  (() #t)
	  (((? symbol? _) sel ...)
	   (check sel (+ i 1)))
	  ((not-a-symbol _ ...)
	   (raise-exception
	    (make-exception
	     (make-error)
	     (make-exception-with-irritants (list i not-a-symbol))
	     (make-exception-with-message
	      (format #f "at least one item of the list of selectors is not a symbol (first: ~a is ~s)."
		      i not-a-symbol)))))))
      (unless (symbol? (current-dkim-selector s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (current-dkim-selector s)))
	  (make-exception-with-message
	   (format #f "the current DKIM selector is not a symbol (~s)."
		   (current-dkim-selector s))))))
      (unless (memq (current-dkim-selector s)
		    (selectors s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (current-dkim-selector s)
					       (selectors s)))
	  (make-exception-with-message
	   (format #f "the current DKIM selector ~s is not part of the list of selectors ~s."
		   (current-dkim-selector s)
		   (selectors s))))))
      (unless (string? (current-dkim-private-key s))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (current-dkim-private-key s)))
	  (make-exception-with-message
	   (format #f "the current DKIM private key is not a string.")))))
      (unless (or (not (expired-dkim-private-key s))
		  (string? (expired-dkim-private-key s)))
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-irritants (list (current-dkim-private-key s)))
	  (make-exception-with-message
	   (format #f "the expired DKIM private key is set, but not a string.")))))))
  s)