Skip to content

Commit

Permalink
Revert masked constants (ToolJet#9824)
Browse files Browse the repository at this point in the history
* revert masked constants

* update

* update

* update

* update

* update

* removed validation

* bump version

---------

Co-authored-by: gsmithun4 <[email protected]>
  • Loading branch information
AnantshreeChandola and gsmithun4 authored May 23, 2024
1 parent 3974b00 commit 7c35c5f
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
2 changes: 1 addition & 1 deletion frontend/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
12 changes: 4 additions & 8 deletions frontend/src/Editor/CodeBuilder/CodeHinter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export function CodeHinter({
}) {
const context = useContext(CodeHinterContext);

const hiddenWorkspaceConstantText = 'Workspace constant values are hidden';

const darkMode = localStorage.getItem('darkMode') === 'true';
const options = {
lineNumbers: lineNumbers ?? false,
Expand Down Expand Up @@ -137,8 +135,6 @@ export function CodeHinter({
const isWorkspaceVariable =
typeof currentValue === 'string' && (currentValue.includes('%%client') || currentValue.includes('%%server'));

const isWorkspaceConstant = typeof currentValue === 'string' && currentValue.includes('constants.');

const constantRegex = /{{constants\.([a-zA-Z0-9_]+)}}/g;

const slideInStyles = useSpring({
Expand Down Expand Up @@ -269,7 +265,7 @@ export function CodeHinter({
globalPreviewCopy = preview;
globalErrorCopy = null;
setResolvingError(null);
setResolvedValue(isWorkspaceConstant ? hiddenWorkspaceConstantText : preview);
setResolvedValue(preview);
}

return [globalPreviewCopy, globalErrorCopy];
Expand All @@ -281,7 +277,7 @@ export function CodeHinter({
return () => {
if (enablePreview) {
setPrevCurrentValue(null);
setResolvedValue(isWorkspaceConstant ? hiddenWorkspaceConstantText : globalPreviewCopy);
setResolvedValue(globalPreviewCopy);
setResolvingError(globalErrorCopy);
}
};
Expand Down Expand Up @@ -377,9 +373,9 @@ export function CodeHinter({
<div>
<div className="d-flex my-1">
<div className="flex-grow-1" style={{ fontWeight: 700, textTransform: 'capitalize' }}>
{!isWorkspaceConstant && previewType}
{previewType}
</div>
{isFocused && !isWorkspaceConstant && (
{isFocused && (
<div className="preview-icons position-relative">
<CodeHinter.PopupIcon callback={() => copyToClipboard(content)} icon="copy" tip="Copy to clipboard" />
</div>
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/Editor/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ enablePatches();

const decimalToHex = (alpha) => (alpha === 0 ? '00' : Math.round(255 * alpha).toString(16));

const maskedWorkspaceConstantStr = '**************';

const EditorComponent = (props) => {
const { socket } = createWebsocketConnection(props?.params?.id);
const mounted = useMounted();
Expand Down Expand Up @@ -374,7 +372,8 @@ const EditorComponent = (props) => {
orgEnvironmentConstantService.getAll().then(({ constants }) => {
const orgConstants = {};
constants.map((constant) => {
orgConstants[constant.name] = maskedWorkspaceConstantStr;
const constantValue = constant.values.find((value) => value.environmentName === 'production')['value'];
orgConstants[constant.name] = constantValue;
});

useCurrentStateStore.getState().actions.setCurrentState({
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/Editor/Viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import DesktopHeader from './Viewer/DesktopHeader';
import './Viewer/viewer.scss';
import useAppDarkMode from '@/_hooks/useAppDarkMode';

const maskedWorkspaceConstantStr = '**************';
class ViewerComponent extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -246,7 +245,7 @@ class ViewerComponent extends React.Component {
if (variablesResult && Array.isArray(variablesResult)) {
variablesResult.map((constant) => {
const constantValue = constant.values.find((value) => value.environmentName === 'production')['value'];
orgConstants[constant.name] = maskedWorkspaceConstantStr;
orgConstants[constant.name] = constantValue;
});
return {
constants: orgConstants,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,7 @@ const verifyConstant = (value, definedConstants) => {
};

const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
const isConstant = type === 'Workspace Constant';
const hiddenWorkspaceConstantText = 'Workspace constant values are hidden';
const invalidConstants = verifyConstant(value, state.constants);
let preview;
let error;
if (invalidConstants?.length) {
[preview, error] = [value, `Undefined constants: ${invalidConstants}`];
} else {
[preview, error] = resolveReferences(value, state, null, {}, true, true);
if (isConstant) {
preview = hiddenWorkspaceConstantText;
}
}
const [preview, error] = resolveReferences(value, state, null, {}, true, true);

const previewType = typeof preview;

Expand All @@ -84,7 +72,7 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
: error?.toString();
const isValidError = error && errorMessage !== 'HiddenEnvironmentVariable';

if (error && (!isValidError || error?.toString().includes('Undefined constants:'))) {
if (error && !isValidError) {
resolvedValue = errorMessage;
}

Expand All @@ -109,6 +97,8 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
}
};

const isConstant = type === 'Workspace Constant';

const [heightRef, currentHeight] = useHeight();

const slideInStyles = useSpring({
Expand All @@ -130,7 +120,7 @@ const ResolvedValue = ({ value, isFocused, state = {}, type }) => {
<div className="alert-banner-type-text">
<div className="d-flex my-1">
<div className="flex-grow-1" style={{ fontWeight: 800, textTransform: 'capitalize' }}>
{isValidError ? 'Error' : isConstant ? null : ` ${type} - ${previewType}`}
{isValidError ? 'Error' : ` ${type} - ${previewType}`}
</div>
</div>
{getPreviewContent(resolvedValue, previewType)}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/_stores/currentStateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const initialState = {
variables: {},
},
succededQuery: {},
constants: {},
};

export const useCurrentStateStore = create(
Expand Down
2 changes: 1 addition & 1 deletion server/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.43.0
2.44.0
26 changes: 5 additions & 21 deletions server/src/controllers/organization_constants.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { OrganizationConstantsAbilityFactory } from 'src/modules/casl/abilities/
import { AppDecorator as App } from 'src/decorators/app.decorator';
import { OrgEnvironmentVariablesAbilityFactory } from 'src/modules/casl/abilities/org-environment-variables-ability.factory';
import { OrgEnvironmentVariable } from 'src/entities/org_envirnoment_variable.entity';
import { OrganizationConstant } from 'src/entities/organization_constants.entity';

@Controller('organization-constants')
export class OrganizationConstantController {
Expand All @@ -32,39 +31,24 @@ export class OrganizationConstantController {

@UseGuards(JwtAuthGuard)
@Get()
async get(@User() user, @Query('decryptValue') decryptValue) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, null);
const decrypt =
decryptValue === 'true' &&
(ability.can('createOrganizationConstant', OrganizationConstant) ||
ability.can('deleteOrganizationConstant', OrganizationConstant));
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, decrypt);
async get(@User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId);
return { constants: result };
}

@UseGuards(IsPublicGuard)
@Get(':app_slug')
async getConstantsFromApp(@App() app, @User() user) {
const result = await this.organizationConstantsService.allEnvironmentConstants(app.organizationId, false);
const result = await this.organizationConstantsService.allEnvironmentConstants(app.organizationId);
return { constants: result };
}

@UseGuards(JwtAuthGuard)
@Get('/environment/:environmentId')
async getConstantsFromEnvironment(
@User() user,
@Param('environmentId') environmentId,
@Query('decryptValue') decryptValue
) {
const ability = await this.organizationConstantsAbilityFactory.organizationConstantActions(user, null);
const decrypt =
decryptValue === 'true' &&
(ability.can('createOrganizationConstant', OrganizationConstant) ||
ability.can('deleteOrganizationConstant', OrganizationConstant));
async getConstantsFromEnvironment(@User() user, @Param('environmentId') environmentId) {
const result = await this.organizationConstantsService.getConstantsForEnvironment(
user.organizationId,
environmentId,
decrypt
environmentId
);
return { constants: result };
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/data_queries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class DataQueriesService {

if (variables?.length > 0) {
for (const variable of variables) {
object = object.replace(variable, await this.resolveConstants(variable, organization_id, environmentId));
object = object.replace(variable, options[variable]);
}
}
return object;
Expand Down
38 changes: 8 additions & 30 deletions server/src/services/organization_constants.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class OrganizationConstantsService {
private appEnvironmentService: AppEnvironmentService
) {}

async allEnvironmentConstants(organizationId: string, decryptValue: boolean): Promise<OrganizationConstant[]> {
async allEnvironmentConstants(organizationId: string): Promise<OrganizationConstant[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const query = manager
.createQueryBuilder(OrganizationConstant, 'organization_constants')
Expand All @@ -33,21 +33,11 @@ export class OrganizationConstantsService {
appEnvironments.map(async (env) => {
const value = constant.orgEnvironmentConstantValues.find((value) => value.environmentId === env.id);

const valueResult = {
return {
environmentName: env.name,
id: value ? value.environmentId : undefined, // Safeguard for undefined 'value'
value: value && value.value.length > 0 ? await this.decryptSecret(organizationId, value.value) : '',
id: value.environmentId,
};

if (value && value.value.length > 0) {
const decryptedOrRawValue = decryptValue
? await this.decryptSecret(organizationId, value.value)
: value.value;

if (decryptValue) {
valueResult['value'] = decryptedOrRawValue;
}
}
return valueResult;
})
);

Expand All @@ -64,11 +54,7 @@ export class OrganizationConstantsService {
});
}

async getConstantsForEnvironment(
organizationId: string,
environmentId: string,
decryptValue: boolean
): Promise<OrganizationConstant[]> {
async getConstantsForEnvironment(organizationId: string, environmentId: string): Promise<OrganizationConstant[]> {
return await dbTransactionWrap(async (manager: EntityManager) => {
const query = manager
.createQueryBuilder(OrganizationConstant, 'organization_constants')
Expand All @@ -78,20 +64,12 @@ export class OrganizationConstantsService {
const result = await query.getMany();

const constantsWithValues = result.map(async (constant) => {
const constantResult = {
const decryptedValue = await this.decryptSecret(organizationId, constant.orgEnvironmentConstantValues[0].value);
return {
id: constant.id,
name: constant.constantName,
value: decryptedValue,
};

if (decryptValue && constant.orgEnvironmentConstantValues.length > 0) {
const decryptedValue = await this.decryptSecret(
organizationId,
constant.orgEnvironmentConstantValues[0].value
);
constantResult['value'] = decryptedValue;
}

return constantResult;
});

return Promise.all(constantsWithValues);
Expand Down

0 comments on commit 7c35c5f

Please sign in to comment.