Repository: hashicorp/vault
File: ui/app/components/mount-backend/type-form.js (JavaScript)

Code before:
 */

import Component from '@glimmer/component';
import { service } from '@ember/service';
import { ALL_ENGINES } from 'vault/utils/all-engines-metadata';

/**
 *
 * @module MountBackendTypeForm
 * MountBackendTypeForm components are used to display type options for
 * mounting either an auth method or secret engine.
 *
 * @example
 * ```js
 * <MountBackend::TypeForm @setMountType={{this.setMountType}} @mountType="secret" />
 * ```
 * @param {CallableFunction} setMountType - function will receive the mount type string. Should update the model type value
 * @param {string} [mountType=auth] - mount type can be `auth` or `secret`
 */

export default class MountBackendTypeForm extends Component {
  @service version;

  get secretEngines() {
    return this.version.isEnterprise
      ? ALL_ENGINES.filter((engine) => engine.mountType !== 'auth')
      : ALL_ENGINES.filter((engine) => engine.mountType !== 'auth' && !engine.requiresEnterprise);
  }

  get authMethods() {
    return this.version.isEnterprise
      ? ALL_ENGINES.filter((engine) => engine.mountType !== 'secret')
      : ALL_ENGINES.filter((engine) => engine.mountType !== 'secret' && !engine.requiresEnterprise);
  }

  get mountTypes() {
    return this.args.mountType === 'secret' ? this.secretEngines : this.authMethods;
  }
}


Review comment (security):
What about engines that support both secret and auth types?

Currently, it's not intuitive that “both” is a valid option, and I suspect this could cause confusion in other parts of the code as well.

I suggest updating the mountType in `all-engines-metadata.js` to be an array instead of a single string. For example:
```
mountType: ['secret', 'auth']
```
Then, wherever we need to check a type, we can use .includes() instead of checking for an exact string.

What do you think about this approach?

Code after:
 */

import Component from '@glimmer/component';
import { service } from '@ember/service';
import { filterEnginesByMountCategory } from 'vault/utils/all-engines-metadata';

/**
 *
 * @module MountBackendTypeForm
 * MountBackendTypeForm components are used to display type options for
 * mounting either an auth method or secret engine.
 *
 * @example
 * ```js
 * <MountBackend::TypeForm @setMountType={{this.setMountType}} @mountCategory="secret" />
 * ```
 * @param {CallableFunction} setMountType - function will receive the mount type string. Should update the model type value
 * @param {string} [mountCategory=auth] - mount category type can be `auth` or `secret`
 */

export default class MountBackendTypeForm extends Component {
  @service version;

  get secretEngines() {
    // If an enterprise license is present, return all secret engines;
    // otherwise, return only the secret engines supported in OSS.
    return filterEnginesByMountCategory({ mountCategory: 'secret', isEnterprise: this.version.isEnterprise });
  }

  get authMethods() {
    // If an enterprise license is present, return all auth methods;
    // otherwise, return only the auth methods supported in OSS.
    return filterEnginesByMountCategory({ mountCategory: 'auth', isEnterprise: this.version.isEnterprise });
  }

  get mountTypes() {
    return this.args.mountCategory === 'secret' ? this.secretEngines : this.authMethods;
  }
}