File: test_audit_api_message.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (151 lines) | stat: -rw-r--r-- 5,516 bytes parent folder | download
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
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License, version 2.0, for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <ctype.h>
#include <m_string.h>
#include <my_compiler.h>
#include <mysql/components/component_implementation.h>
#include <mysql/components/service_implementation.h>
#include <mysql/components/services/audit_api_message_service.h>
#include <mysql/components/services/udf_registration.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <string>

REQUIRES_SERVICE_PLACEHOLDER(udf_registration);
REQUIRES_SERVICE_PLACEHOLDER(mysql_audit_api_message);

/**
  Implements test_audit_api_message_internal UDF. This function generates
  AUDIT_API_MESSAGE_INTERNAL event of the AUDIT_API_MESSAGE_CLASS class.

  Although AUDIT_API_MESSAGE_INTERNAL message is generated as the result
  of the user interaction, this should not be done in the production
  environment. AUDIT_API_MESSAGE_INTERNAL message should be generated as
  as the result of the internal processing, such as background threads,
  timers etc.

  @param init       Unused.
  @param args       Unused.
  @param null_value Unused.
  @param error      Unused.

  @retval 0 This function always returns 0.
*/
static long long message_internal(UDF_INIT *init [[maybe_unused]],
                                  UDF_ARGS *args [[maybe_unused]],
                                  unsigned char *null_value [[maybe_unused]],
                                  unsigned char *error [[maybe_unused]]) {
  mysql_event_message_key_value_t val;

  lex_cstring_set(&val.key, "my_numeric_key");
  val.value_type = MYSQL_AUDIT_MESSAGE_VALUE_TYPE_NUM;
  val.value.num = INT_MIN64;

  mysql_service_mysql_audit_api_message->emit(
      MYSQL_AUDIT_MESSAGE_INTERNAL, STRING_WITH_LEN("test_audit_api_message"),
      STRING_WITH_LEN("test_audit_api_message"),
      STRING_WITH_LEN("test_audit_api_message_internal"), &val, 1);

  return 0;
}

/**
  Implements test_audit_api_message_internal UDF. This function generates
  AUDIT_API_MESSAGE_USER event of the AUDIT_API_MESSAGE_CLASS class.

  @param init       Unused.
  @param args       Unused.
  @param null_value Unused.
  @param error      Unused.

  @retval 0 This function always returns 0.
*/
static long long message_user(UDF_INIT *init [[maybe_unused]],
                              UDF_ARGS *args [[maybe_unused]],
                              unsigned char *null_value [[maybe_unused]],
                              unsigned char *error [[maybe_unused]]) {
  mysql_event_message_key_value_t val;

  lex_cstring_set(&val.key, "my_string_key");
  val.value_type = MYSQL_AUDIT_MESSAGE_VALUE_TYPE_STR;
  lex_cstring_set(&val.value.str, "my_string_value");

  mysql_service_mysql_audit_api_message->emit(
      MYSQL_AUDIT_MESSAGE_USER, STRING_WITH_LEN("test_audit_api_message"),
      STRING_WITH_LEN("test_audit_api_message"),
      STRING_WITH_LEN("test_audit_api_message_user"), &val, 1);

  return 0;
}

static mysql_service_status_t init() {
  if (mysql_service_udf_registration->udf_register(
          "test_audit_api_message_internal", INT_RESULT,
          (Udf_func_any)message_internal, nullptr, nullptr))
    return true;

  if (mysql_service_udf_registration->udf_register(
          "test_audit_api_message_user", INT_RESULT, (Udf_func_any)message_user,
          nullptr, nullptr)) {
    int was_present = 0;

    mysql_service_udf_registration->udf_unregister(
        "test_audit_api_message_internal", &was_present);

    return true;
  }

  return false;
}

static mysql_service_status_t deinit() {
  int was_present = 0;

  mysql_service_udf_registration->udf_unregister(
      "test_audit_api_message_internal", &was_present);

  mysql_service_udf_registration->udf_unregister("test_audit_api_message_user",
                                                 &was_present);

  return false;
}

BEGIN_COMPONENT_PROVIDES(test_audit_api_message)
END_COMPONENT_PROVIDES();

BEGIN_COMPONENT_REQUIRES(test_audit_api_message)
REQUIRES_SERVICE(mysql_audit_api_message), REQUIRES_SERVICE(udf_registration),
    END_COMPONENT_REQUIRES();

BEGIN_COMPONENT_METADATA(test_audit_api_message)
METADATA("mysql.author", "Oracle Corporation"),
    METADATA("mysql.license", "GPL"), END_COMPONENT_METADATA();

DECLARE_COMPONENT(test_audit_api_message, "test_audit_api_message")
init, deinit END_DECLARE_COMPONENT();

DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_audit_api_message)
    END_DECLARE_LIBRARY_COMPONENTS