Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cira timestamps to db #1153

Merged
merged 6 commits into from
Nov 20, 2023
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
3 changes: 2 additions & 1 deletion .mpsrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"consul_enabled": false,
"consul_host": "localhost",
"consul_port": "8500",
"consul_key_prefix": "MPS"
"consul_key_prefix": "MPS",
"cira_last_seen": true
}
3 changes: 3 additions & 0 deletions data/initMPS.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ CREATE TABLE IF NOT EXISTS devices(
tenantid varchar(36) NOT NULL,
friendlyname varchar(256),
dnssuffix varchar(256),
lastconnected timestamp with time zone,
lastseen timestamp with time zone,
lastdisconnected timestamp with time zone,
deviceinfo JSON,
CONSTRAINT device_guid UNIQUE(guid),
PRIMARY KEY (guid,tenantid)
Expand Down
69 changes: 69 additions & 0 deletions src/amt/APFProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Buffer } from 'node:buffer'
import Common from '../utils/common'
import { logger } from '../logging'
import APFProcessor, { APFProtocol } from './APFProcessor'
import { Environment } from '../utils/Environment'
import { type CIRASocket } from '../models/models'
import { type CIRAChannel } from './CIRAChannel'
import { EventEmitter } from 'stream'
Expand Down Expand Up @@ -1008,6 +1009,74 @@ describe('APFProcessor Tests', () => {
tag: {},
write: jest.fn()
} as any
const config = {
common_name: 'localhost',
port: 4433,
country: 'US',
company: 'NoCorp',
listen_any: true,
tls_offload: false,
web_port: 3000,
generate_certificates: true,
secrets_provider: 'string',
tls_cert: 'string',
tls_cert_key: 'string',
tls_cert_ca: 'string',
web_admin_user: 'admin',
web_admin_password: 'password',
web_auth_enabled: true,
vault_address: 'http://localhost:8200',
vault_token: 'myroot',
mqtt_address: '',
secrets_path: 'secret/data/',
cert_format: 'file',
data_path: '../private/data.json',
cert_path: '../private',
jwt_secret: 'secret',
jwt_issuer: '9EmRJTbIiIb4bIeSsmgcWIjrR6HyETqc',
jwt_expiration: 1440,
web_tls_cert: null,
web_tls_cert_key: null,
web_tls_cert_ca: null,
redirection_expiration_time: null,
cors_origin: '*',
cors_headers: '*',
cors_methods: '*',
jwt_token_header: null,
jwt_tenant_property: null,
db_provider: 'postgres',
connection_string: 'postgresql://<USERNAME>:<PASSWORD>@localhost:5432/mpsdb?sslmode=no-verify',
instance_name: 'localhost',
mps_tls_config: {
key: '../private/mpsserver-cert-private.key',
cert: '../private/mpsserver-cert-public.crt',
requestCert: true,
rejectUnauthorized: false,
minVersion: 'TLSv1',
ciphers: null,
secureOptions: ['SSL_OP_NO_SSLv2', 'SSL_OP_NO_SSLv3']
},
web_tls_config: {
key: '../private/mpsserver-cert-private.key',
cert: '../private/mpsserver-cert-public.crt',
ca: ['../private/root-cert-public.crt'],
secureOptions: [
'SSL_OP_NO_SSLv2',
'SSL_OP_NO_SSLv3',
'SSL_OP_NO_COMPRESSION',
'SSL_OP_CIPHER_SERVER_PREFERENCE',
'SSL_OP_NO_TLSv1',
'SSL_OP_NO_TLSv11'
]
},
consul_enabled: true,
consul_host: 'localhost',
consul_port: '8500',
consul_key_prefix: 'MPS',
cira_last_seen: true
}
Environment.Config = config

const length = 5
const data = ''
const sendKeepAliveReplySpy = jest.spyOn(APFProcessor, 'SendKeepAliveReply')
Expand Down
5 changes: 5 additions & 0 deletions src/amt/APFProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Common from '../utils/common'
import { type CIRASocket } from '../models/models'
import { type CIRAChannel } from './CIRAChannel'
import { EventEmitter } from 'stream'
import { Environment } from '../utils/Environment'

const KEEPALIVE_INTERVAL = 30 // 30 seconds is typical keepalive interval for AMT CIRA connection

export enum APFProtocol {
Expand Down Expand Up @@ -422,6 +424,9 @@ const APFProcessor = {
if (length < 5) {
return 0
}
if (Environment.Config.cira_last_seen) {
APFProcessor.APFEvents.emit('keepAliveRequest', socket.tag.nodeid)
}
logger.verbose(`${messages.MPS_KEEPALIVE_REQUEST}: ${socket.tag.nodeid}`)
APFProcessor.SendKeepAliveReply(socket, Common.ReadInt(data, 1))
return 5
Expand Down
1 change: 1 addition & 0 deletions src/amt/ConnectedDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class ConnectedDevice {
limiter: Bottleneck
kvmConnect: boolean
tenantId: string
lastKeepAlive: Date

constructor (ciraSocket: CIRASocket, readonly username: string, readonly password: string, tenantId: string, httpHandler: HttpHandler = new HttpHandler(), kvmConnect: boolean = false, limiter: Bottleneck = new Bottleneck({
maxConcurrent: 3,
Expand Down
Loading
Loading