Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions ui/src/components/SnmpConfiguration/SnmpConfigDetailsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ import { DEFAULT_MONITORING_LOCATION, DEFAULT_SNMP_V3_SECURITY_LEVEL } from '@/l
import { getDefaultSnmpBaseConfiguration, useSnmpConfigStore } from '@/stores/snmpConfigStore'
import { SnmpAgentConfig, SnmpBaseConfiguration, SnmpConfigFormErrors, SnmpFieldInfo } from '@/types/snmpConfig'
import { validateDefinition, SecurityLevelSelectionOptions, SnmpAuthProtocols, SnmpPrivacyProtocols } from '@/lib/snmpValidator'
import { withDefaultHints } from '@/lib/snmpConfigHelpers'
import SnmpConfigPairedFieldInputs from './SnmpConfigPairedFieldInputs.vue'
import ScvSearchDrawer from '../SCV/ScvSearchDrawer.vue'
import { ScvSearchItem } from '@/types/scv'
Expand Down Expand Up @@ -301,49 +302,53 @@ const displaySnmp3Params = computed(() => {
})

// Field metadata for v-for rendering using SnmpConfigPairedFieldInputs
const generalParamFields: SnmpFieldInfo[] = [
const generalParamFields = computed<SnmpFieldInfo[]>(() => withDefaultHints([
{ key: 'timeout', label: 'Timeout', hint: 'Timeout in milliseconds', dataTest: 'snmp-definition-timeout', isNumeric: true },
{ key: 'retry', label: 'Retries', hint: 'Number of retries', dataTest: 'snmp-definition-retry', isNumeric: true }
]
], store.currentDefaults))

const advancedConfigOptions: SnmpFieldInfo[] = [
{ key: 'port', label: 'Port', hint: 'SNMP port (default: 161)', dataTest: 'snmp-definition-port', isNumeric: true },
const advancedConfigOptions = computed<SnmpFieldInfo[]>(() => withDefaultHints([
{ key: 'port', label: 'Port', hint: 'SNMP port', dataTest: 'snmp-definition-port', isNumeric: true },
{ key: 'proxyHost', label: 'Proxy Host', hint: 'Proxy host for SNMP communication', dataTest: 'snmp-definition-proxy-host' },
{ key: 'maxRequestSize', label: 'Max Request Size', hint: 'Maximum bytes per PDU request', dataTest: 'snmp-definition-max-request-size', isNumeric: true },
{ key: 'maxVarsPerPdu', label: 'Max Vars Per PDU', hint: 'Variables per SNMP request', dataTest: 'snmp-definition-max-vars-per-pdu', isNumeric: true },
{ key: 'maxRepetitions', label: 'Max Repetitions', hint: 'Repetitions per get-bulk request', dataTest: 'snmp-definition-max-repetitions', isNumeric: true },
{ key: 'ttl', label: 'TTL', hint: 'Time to live', dataTest: 'snmp-definition-ttl', isNumeric: true }
]
], store.currentDefaults))

const snmpV2Fields: SnmpFieldInfo[] = [
const snmpV2Fields = computed<SnmpFieldInfo[]>(() => withDefaultHints([
{ key: 'readCommunity', label: 'Read Community String', hint: 'Read community string', dataTest: 'snmp-lookup-read-community', scvEnabled: true },
{ key: 'writeCommunity', label: 'Write Community String', hint: 'Write community string', dataTest: 'snmp-lookup-write-community', scvEnabled: true }
]
], store.currentDefaults))

const snmpV3Fields: SnmpFieldInfo[] = [
const snmpV3Fields = computed<SnmpFieldInfo[]>(() => withDefaultHints([
{ key: 'securityName', label: 'Security Name', hint: 'SNMP v3 security name', dataTest: 'snmp-definition-security-name', scvEnabled: true },
{
key: 'securityLevel', label: 'Security Level', hint: 'SNMP v3 security level', dataTest: 'snmp-definition-security-level', isNumeric: true,
isSelect: true, selectOptions: SecurityLevelSelectionOptions
},
{ key: 'authPassphrase', label: 'Auth Passphrase', hint: 'Authentication passphrase', dataTest: 'snmp-definition-auth-passphrase', scvEnabled: true },
{ key: 'authPassphrase', label: 'Auth Passphrase', hint: 'Authentication passphrase', dataTest: 'snmp-definition-auth-passphrase', scvEnabled: true,
skipDefaultHint: true
},
{
key: 'authProtocol', label: 'Auth Protocol', hint: 'Authentication protocol', dataTest: 'snmp-definition-auth-protocol',
isSelect: true, selectOptions: SnmpAuthProtocols.map(protocol => ({ _text: protocol, _value: protocol }))
},
{ key: 'privacyPassphrase', label: 'Privacy Passphrase', hint: 'Privacy passphrase', dataTest: 'snmp-definition-privacy-passphrase', scvEnabled: true },
{ key: 'privacyPassphrase', label: 'Privacy Passphrase', hint: 'Privacy passphrase', dataTest: 'snmp-definition-privacy-passphrase', scvEnabled: true,
skipDefaultHint: true
},
{
key: 'privacyProtocol', label: 'Privacy Protocol', hint: 'Privacy protocol', dataTest: 'snmp-definition-privacy-protocol',
isSelect: true, selectOptions: SnmpPrivacyProtocols.map(protocol => ({ _text: protocol, _value: protocol }))
}
]
], store.currentDefaults))

const snmpV3ContextFields: SnmpFieldInfo[] = [
const snmpV3ContextFields = computed<SnmpFieldInfo[]>(() => withDefaultHints([
{ key: 'engineId', label: 'Engine ID', hint: 'SNMP engine ID', dataTest: 'snmp-definition-engine-id' },
{ key: 'contextEngineId', label: 'Context Engine ID', hint: 'Context engine ID', dataTest: 'snmp-definition-context-engine-id' },
{ key: 'contextName', label: 'Context Name', hint: 'SNMP context name', dataTest: 'snmp-definition-context-name' },
{ key: 'enterpriseId', label: 'Enterprise ID', hint: 'Enterprise ID', dataTest: 'snmp-definition-enterprise-id' }
]
], store.currentDefaults))

const loadInitialValues = () => {
const currentConfig: SnmpAgentConfig = props.config ?? getDefaultSnmpBaseConfiguration()
Expand Down
42 changes: 42 additions & 0 deletions ui/src/lib/snmpConfigHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
///
/// Licensed to The OpenNMS Group, Inc (TOG) under one or more
/// contributor license agreements. See the LICENSE.md file
/// distributed with this work for additional information
/// regarding copyright ownership.
///
/// TOG licenses this file to You under the GNU Affero General
/// Public License Version 3 (the "License") or (at your option)
/// any later version. You may not use this file except in
/// compliance with the License. You may obtain a copy of the
/// License at:
///
/// https://www.gnu.org/licenses/agpl-3.0.txt
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
/// either express or implied. See the License for the specific
/// language governing permissions and limitations under the
/// License.
///

import { SnmpBaseConfiguration, SnmpFieldInfo } from '@/types/snmpConfig'

/**
* Augments each field's hint with the current default value, unless the field
* has `skipDefaultHint: true` (used for sensitive fields such as passphrases)
* or the default value is absent/empty.
*/
export const withDefaultHints = (fields: SnmpFieldInfo[], defaults: SnmpBaseConfiguration): SnmpFieldInfo[] => {
const defaultsMap = defaults as Record<string, unknown>
return fields.map(field => {
const value = defaultsMap[field.key]
if (value === undefined || value === null || value === '' || field.skipDefaultHint) {
return field
}
return {
...field,
hint: field.hint ? `${field.hint}. Current default value is: '${value}'.` : `Current default value is: '${value}'.`
}
})
}
27 changes: 26 additions & 1 deletion ui/src/stores/snmpConfigStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
///

import { defineStore } from 'pinia'
import { DEFAULT_MONITORING_LOCATION, DEFAULT_SNMP_MAX_REPETITIONS, DEFAULT_SNMP_MAX_REQUEST_SIZE, DEFAULT_SNMP_MAX_VARS_PER_PDU, DEFAULT_SNMP_PORT, DEFAULT_SNMP_RETRIES, DEFAULT_SNMP_TIMEOUT, DEFAULT_SNMP_TTL, DEFAULT_SNMP_V3_AUTH_PASSPHRASE, DEFAULT_SNMP_V3_AUTH_PROTOCOL, DEFAULT_SNMP_V3_PRIVACY_PASSPHRASE, DEFAULT_SNMP_V3_PRIVACY_PROTOCOL, DEFAULT_SNMP_V3_SECURITY_LEVEL, DEFAULT_SNMP_V3_SECURITY_NAME, DEFAULT_SNMP_VERSION } from '@/lib/constants'
import { DEFAULT_MONITORING_LOCATION, DEFAULT_SNMP_MAX_REPETITIONS, DEFAULT_SNMP_MAX_REQUEST_SIZE, DEFAULT_SNMP_MAX_VARS_PER_PDU, DEFAULT_SNMP_PORT, DEFAULT_SNMP_READ_COMMUNITY_STRING, DEFAULT_SNMP_RETRIES, DEFAULT_SNMP_TIMEOUT, DEFAULT_SNMP_TTL, DEFAULT_SNMP_V3_AUTH_PASSPHRASE, DEFAULT_SNMP_V3_AUTH_PROTOCOL, DEFAULT_SNMP_V3_PRIVACY_PASSPHRASE, DEFAULT_SNMP_V3_PRIVACY_PROTOCOL, DEFAULT_SNMP_V3_SECURITY_LEVEL, DEFAULT_SNMP_V3_SECURITY_NAME, DEFAULT_SNMP_VERSION, DEFAULT_SNMP_WRITE_COMMUNITY_STRING } from '@/lib/constants'
import { getDefaultSnmpSecurityLevel, isValidSnmpSecurityLevel } from '@/lib/snmpValidator'
import { getMonitoringLocations } from '@/services/monitoringLocationService'
import { deleteSnmpDefinition, deleteSnmpProfile, getSnmpConfig, lookupSnmpConfig, saveSnmpDefinition, saveSnmpProfile } from '@/services/snmpConfigService'
Expand Down Expand Up @@ -290,6 +290,30 @@ export const useSnmpConfigStore = defineStore('useSnmpConfigStore', () => {
return resp
}

const currentDefaults = computed<SnmpBaseConfiguration>(() => ({
readCommunity: config.value.readCommunity ?? DEFAULT_SNMP_READ_COMMUNITY_STRING,
writeCommunity: config.value.writeCommunity ?? DEFAULT_SNMP_WRITE_COMMUNITY_STRING,
timeout: config.value.timeout ?? DEFAULT_SNMP_TIMEOUT,
retry: config.value.retry ?? DEFAULT_SNMP_RETRIES,
port: config.value.port ?? DEFAULT_SNMP_PORT,
maxRequestSize: config.value.maxRequestSize ?? DEFAULT_SNMP_MAX_REQUEST_SIZE,
maxVarsPerPdu: config.value.maxVarsPerPdu ?? DEFAULT_SNMP_MAX_VARS_PER_PDU,
maxRepetitions: config.value.maxRepetitions ?? DEFAULT_SNMP_MAX_REPETITIONS,
ttl: config.value.ttl ?? DEFAULT_SNMP_TTL,
version: config.value.version ?? DEFAULT_SNMP_VERSION,
securityName: config.value.securityName ?? DEFAULT_SNMP_V3_SECURITY_NAME,
securityLevel: config.value.securityLevel ?? DEFAULT_SNMP_V3_SECURITY_LEVEL,
authPassphrase: config.value.authPassphrase ?? DEFAULT_SNMP_V3_AUTH_PASSPHRASE,
authProtocol: config.value.authProtocol ?? DEFAULT_SNMP_V3_AUTH_PROTOCOL,
privacyPassphrase: config.value.privacyPassphrase ?? DEFAULT_SNMP_V3_PRIVACY_PASSPHRASE,
privacyProtocol: config.value.privacyProtocol ?? DEFAULT_SNMP_V3_PRIVACY_PROTOCOL,
proxyHost: config.value.proxyHost,
engineId: config.value.engineId,
contextEngineId: config.value.contextEngineId,
contextName: config.value.contextName,
enterpriseId: config.value.enterpriseId
}))

return {
activeTab,
activeViewConfigurationsTab,
Expand All @@ -298,6 +322,7 @@ export const useSnmpConfigStore = defineStore('useSnmpConfigStore', () => {
definitionCreateEditMode,
deleteProfile,
fetchMonitoringLocations,
currentDefaults,
isLoading,
lookupIpAddress,
monitoringLocations,
Expand Down
4 changes: 4 additions & 0 deletions ui/src/types/snmpConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,8 @@ export interface SnmpFieldInfo {
// options for the select dropdown. _text is display label, _value is the underlying value.
// _value should be a string even if isNumeric is true
selectOptions?: ISelectItemType[]

// if true, just display the 'hint', do not display the default value.
// this is useful for fields that might display sensitive information, such as passwords or passphrases.
skipDefaultHint?: boolean
}
127 changes: 127 additions & 0 deletions ui/tests/lib/snmpConfigHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
///
/// Licensed to The OpenNMS Group, Inc (TOG) under one or more
/// contributor license agreements. See the LICENSE.md file
/// distributed with this work for additional information
/// regarding copyright ownership.
///
/// TOG licenses this file to You under the GNU Affero General
/// Public License Version 3 (the "License") or (at your option)
/// any later version. You may not use this file except in
/// compliance with the License. You may obtain a copy of the
/// License at:
///
/// https://www.gnu.org/licenses/agpl-3.0.txt
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
/// either express or implied. See the License for the specific
/// language governing permissions and limitations under the
/// License.
///

import { describe, it, expect } from 'vitest'
import { withDefaultHints } from '@/lib/snmpConfigHelpers'
import { SnmpBaseConfiguration, SnmpFieldInfo } from '@/types/snmpConfig'

const defaults: SnmpBaseConfiguration = {
timeout: 3000,
retry: 1,
port: 161,
readCommunity: 'public',
writeCommunity: 'private',
authPassphrase: '0p3nNMSv3',
privacyPassphrase: '0p3nNMSv3'
}

const field = (overrides: Partial<SnmpFieldInfo>): SnmpFieldInfo => ({
key: 'timeout',
label: 'Timeout',
dataTest: 'test-timeout',
...overrides
})

describe('withDefaultHints', () => {
it('appends default value to existing hint', () => {
const result = withDefaultHints([
field({ key: 'timeout', hint: 'Timeout in milliseconds' })
], defaults)
expect(result[0].hint).toBe("Timeout in milliseconds. Current default value is: '3000'.")
})

it('sets hint when field has no existing hint', () => {
const result = withDefaultHints([
field({ key: 'port', hint: undefined })
], defaults)
expect(result[0].hint).toBe("Current default value is: '161'.")
})

it('does not modify hint when default value is undefined', () => {
const result = withDefaultHints([
field({ key: 'proxyHost', hint: 'Proxy host' })
], defaults)
expect(result[0].hint).toBe('Proxy host')
})

it('does not modify hint when default value is null', () => {
const nullDefaults = { ...defaults, timeout: null as unknown as number }
const result = withDefaultHints([
field({ key: 'timeout', hint: 'Timeout in milliseconds' })
], nullDefaults)
expect(result[0].hint).toBe('Timeout in milliseconds')
})

it('does not modify hint when default value is empty string', () => {
const emptyDefaults = { ...defaults, readCommunity: '' }
const result = withDefaultHints([
field({ key: 'readCommunity', hint: 'Read community string' })
], emptyDefaults)
expect(result[0].hint).toBe('Read community string')
})

it('does not append hint when skipDefaultHint is true', () => {
const result = withDefaultHints([
field({ key: 'authPassphrase', hint: 'Authentication passphrase', skipDefaultHint: true })
], defaults)
expect(result[0].hint).toBe('Authentication passphrase')
})

it('suppresses hint entirely when skipDefaultHint is true and no existing hint', () => {
const result = withDefaultHints([
field({ key: 'privacyPassphrase', hint: undefined, skipDefaultHint: true })
], defaults)
expect(result[0].hint).toBeUndefined()
})

it('does not expose passphrase values in hints', () => {
const result = withDefaultHints([
field({ key: 'authPassphrase', hint: 'Authentication passphrase', skipDefaultHint: true }),
field({ key: 'privacyPassphrase', hint: 'Privacy passphrase', skipDefaultHint: true })
], defaults)
expect(result[0].hint).not.toContain('0p3nNMSv3')
expect(result[1].hint).not.toContain('0p3nNMSv3')
})

it('returns original field reference when no modification is needed', () => {
const original = field({ key: 'proxyHost', hint: 'Proxy host' })
const result = withDefaultHints([original], defaults)
expect(result[0]).toBe(original)
})

it('returns a new object when hint is modified', () => {
const original = field({ key: 'timeout', hint: 'Timeout in milliseconds' })
const result = withDefaultHints([original], defaults)
expect(result[0]).not.toBe(original)
})

it('processes multiple fields correctly', () => {
const result = withDefaultHints([
field({ key: 'timeout', hint: 'Timeout in milliseconds' }),
field({ key: 'retry', hint: 'Number of retries' }),
field({ key: 'authPassphrase', hint: 'Authentication passphrase', skipDefaultHint: true })
], defaults)
expect(result[0].hint).toBe("Timeout in milliseconds. Current default value is: '3000'.")
expect(result[1].hint).toBe("Number of retries. Current default value is: '1'.")
expect(result[2].hint).toBe('Authentication passphrase')
})
})
Loading
Loading