0

Variable expiration handler

by
Published today

Workspace variable expiration handler sending a message to a Microsoft Teams channel an hour before the value of a variable expires, so it can be rotated before it stops working

Script teams Verified

The script

Submitted by hugo989 Bun
Verified 3 hours ago
1
import dayjs from 'dayjs'
2
import * as wmill from "windmill-client"
3

4
// The variable's value is deliberately not a parameter: job arguments and results are
5
// stored in cleartext and shown in Runs, so this handler is given a path to rotate and
6
// never a value to handle. Do not add one.
7
export async function main(
8
  workspace_id: string, // The workspace the variable belongs to
9
  variable_path: string, // The path of the variable whose value is expiring
10
  description: string, // The variable's description
11
  value_expires_at: string, // The datetime at which the value expires
12
  is_secret: boolean, // Whether the variable is a secret
13
  channel: string,
14
) {
15
  const baseUrl = process.env["WM_BASE_URL"];
16
  const variablesUrl = `${baseUrl}/variables?workspace=${encodeURIComponent(workspace_id)}`;
17
  const expiresAt = dayjs(value_expires_at).format("DD.MM.YYYY HH:mm (Z)");
18
  const kind = is_secret ? "Secret" : "Variable";
19

20
  const mdText = `*${kind} [${variable_path}](${variablesUrl}) is about to expire*\n- Expires at: ${expiresAt}`
21

22
  await wmill.TeamsService.sendMessageToConversation(
23
    {
24
      requestBody: {
25
        conversation_id: channel,
26
        success: true,
27
        text: mdText,
28
        card_block: {
29
          "type": "message",
30
          "attachments": [
31
            {
32
              "contentType": "application/vnd.microsoft.card.adaptive",
33
              "content": {
34
                "type": "AdaptiveCard",
35
                "body": [
36
                  {
37
                    "type": "TextBlock",
38
                    "size": "Medium",
39
                    "weight": "Bolder",
40
                    "text": "Variable Expiration Handler"
41
                  },
42
                  {
43
                    "type": "ColumnSet",
44
                    "columns": [
45
                      {
46
                        "type": "Column",
47
                        "items": [
48
                          {
49
                            "type": "Icon",
50
                            "name": "Key",
51
                            "size": "Medium",
52
                            "style": "Filled",
53
                            "color": "Warning"
54
                          }
55
                        ],
56
                        "width": "auto"
57
                      },
58
                      {
59
                        "type": "Column",
60
                        "items": [
61
                          {
62
                            "type": "TextBlock",
63
                            "text": mdText,
64
                            "wrap": true
65
                          },
66
                          {
67
                            "type": "TextBlock",
68
                            "text": description
69
                              ? `Rotate it before it stops working.\n\n${description}`
70
                              : "Rotate it before it stops working.",
71
                            "wrap": true
72
                          }
73
                        ],
74
                        "width": "stretch"
75
                      }
76
                    ]
77
                  }
78
                ],
79
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
80
                "version": "1.6"
81
              }
82
            }
83
          ]
84
        }
85
      }
86
    }
87
  )
88
}
89