001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.ThreadSafe;
023
024/**
025 * Converts non-secure bound parameter values into safe display values for diagnostics.
026 * <p>
027 * This is display-only: it does not change what is bound to the database. Pyranid never invokes a
028 * {@code ParameterRedactor} for {@link SecureParameter} values; {@link SecureParameter#getMask()} always wins. For batch
029 * executions, Pyranid renders a bounded batch summary and does not invoke this redactor for individual batch values.
030 * <p>
031 * Redaction failures are fail-fast. If an implementation throws, Pyranid propagates the failure from the diagnostic
032 * rendering call site.
033 *
034 * @author <a href="https://www.revetkn.com">Mark Allen</a>
035 * @since 4.4.0
036 */
037@ThreadSafe
038@FunctionalInterface
039public interface ParameterRedactor {
040        /**
041         * Redacts a single non-secure parameter for diagnostics.
042         *
043         * @param statementContext current SQL context
044         * @param parameterIndex   zero-based index into {@link StatementContext#getParameters()} after IN-list expansion
045         * @param parameter        parameter value to render safely
046         * @return the safe display value
047         */
048        @Nullable
049        Object redactParameter(@NonNull StatementContext<?> statementContext,
050                                                                                                 int parameterIndex,
051                                                                                                 @Nullable Object parameter);
052
053        /**
054         * Acquires the default redactor, which renders non-secure values verbatim for non-batch executions.
055         *
056         * @return the default redactor
057         */
058        @NonNull
059        static ParameterRedactor none() {
060                return (statementContext, parameterIndex, parameter) -> parameter;
061        }
062
063        /**
064         * Acquires a redactor that masks every non-secure value as {@code <redacted>}.
065         *
066         * @return a redactor that masks every non-secure value
067         */
068        @NonNull
069        static ParameterRedactor redactAll() {
070                return (statementContext, parameterIndex, parameter) -> SecureParameterSupport.DEFAULT_MASK;
071        }
072}