Skip to content

Commit 1371902

Browse files
introduce data connectors, used to validate if vin is allowed to connect
1 parent be065fb commit 1371902

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4440
-291
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ jobs:
1717
with:
1818
go-version: 1.20.3
1919

20-
- name: Set up protoc
21-
run: |
22-
wget https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protoc-25.1-linux-x86_64.zip
23-
unzip protoc-25.1-linux-x86_64.zip
24-
sudo mv bin/protoc /usr/local/bin/protoc
25-
sudo mv include/* /usr/local/include/
26-
27-
- name: Install protoc-gen-go
28-
run: go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1
29-
3020
- name: Generated protofiles are up to date
3121
run: |
3222
make generate-protos

Dockerfile.protos

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FROM debian:12.5-slim
2+
3+
WORKDIR /
4+
5+
ENV GO_VERSION=1.20
6+
ENV RUBY_VERSION=3.2
7+
ENV PYTHON_VERSION=3.10
8+
9+
RUN apt-get update && \
10+
apt-get install -y \
11+
git \
12+
wget \
13+
unzip \
14+
ruby-dev \
15+
python3-dev \
16+
python3-pip \
17+
python3-grpcio \
18+
python3-grpc-tools
19+
20+
# protobuf compiler
21+
ENV PROTOC_VERSION=25.1
22+
RUN PROTOC_ZIP=protoc-${PROTOC_VERSION}-linux-x86_64.zip && \
23+
wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/$PROTOC_ZIP -O /tmp/$PROTOC_ZIP && \
24+
unzip /tmp/$PROTOC_ZIP -d /usr/local bin/protoc && \
25+
unzip /tmp/$PROTOC_ZIP -d /usr/local 'include/*' && \
26+
rm -f /tmp/$PROTOC_ZIP
27+
28+
# Go
29+
ENV PATH="/usr/local/go/bin:$PATH"
30+
ENV PATH="/root/go/bin:$PATH"
31+
RUN wget https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz && \
32+
tar -C /usr/local -xzf go$GO_VERSION.linux-amd64.tar.gz && \
33+
rm go$GO_VERSION.linux-amd64.tar.gz
34+
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 && \
35+
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
36+
37+
# Ruby
38+
RUN gem install grpc grpc-tools
39+
40+
ENV PROTO_DIR=./protos
41+
COPY generate_protos.sh /generate_protos.sh
42+
RUN chmod +x /generate_protos.sh
43+
44+
CMD ["/generate_protos.sh"]

Makefile

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,14 @@ generate-certs:
6363
clean:
6464
find $(PROTO_DIR) -type f ! -name '*.proto' -delete
6565

66-
generate-golang:
67-
protoc --go_out=./ --go_opt=paths=source_relative $(PROTO_DIR)/*.proto
66+
generate-mocks:
67+
mockgen -source protos/data_connector_service_grpc.pb.go -destination=test/mocks/data_connector_service_grpc.go -package mocks
6868

69-
generate-python:
70-
protoc -I=$(PROTO_DIR) --python_out=$(PROTO_DIR)/python/ $(PROTO_DIR)/*.proto
71-
72-
generate-ruby:
73-
protoc --ruby_out=$(PROTO_DIR)/ruby/ --proto_path=$(PROTO_DIR) $(PROTO_FILES)
74-
75-
generate-protos: clean generate-golang generate-python generate-ruby
69+
generate-protos: clean
70+
docker run -v ./protos:/protos:rw --rm `docker build -qf Dockerfile.protos .`
7671

7772
image-gen:
7873
docker build -t $(ALPHA_IMAGE_NAME) .
7974
docker save $(ALPHA_IMAGE_NAME) | gzip > $(ALPHA_IMAGE_COMPRESSED_FILENAME).tar.gz
8075

81-
.PHONY: test build vet linters install integration image-gen generate-protos generate-golang generate-python generate-ruby clean
76+
.PHONY: test build vet linters install integration image-gen generate-protos generate-mocks clean

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ The following [dispatchers](./telemetry/producer.go#L10-L19) are supported
160160

161161
>NOTE: To add a new dispatcher, please provide integration tests and updated documentation. To serialize dispatcher data as json instead of protobufs, add a config `transmit_decoded_records` and set value to `true` as shown [here](config/test_configs_test.go#L186)
162162

163+
## Data Connectors
164+
165+
Data connectors can be configured to fetch data from a external sources to enhance server functionality.
166+
167+
**Available capabilities**:
168+
- `vin_allowed`: checks if a vin is allowed to connect to the server. It is recommended to configure this to enhance security.
169+
170+
**Available data connectors**:
171+
- Redis
172+
- GRPC
173+
- HTTP
174+
- File
175+
176+
To learn about configuring, see their [full documentation](./connector/README.md).
177+
163178
## Reliable Acks
164179
Fleet telemetry allows you to send ack messages back to the vehicle. This is useful for applications that need to ensure the data was received and processed. To enable this feature, set `reliable_ack_sources` to one of configured dispatchers (`kafka`,`kinesis`,`pubsub`,`zmq`) in the config file. You can only set reliable acks to one dispatcher per recordType. See [here](./test/integration/config.json#L8) for sample config.
165180

@@ -175,9 +190,8 @@ To suppress [tls handshake error logging](https://cs.opensource.google/go/go/+/m
175190
## Protos
176191
Data is encapsulated into protobuf messages of different types. Protos can be recompiled via:
177192

178-
1. Install protoc, currently on version 4.25.1: https://grpc.io/docs/protoc-installation/
179-
2. Install protoc-gen-go: `go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28`
180-
3. Run make command
193+
1. Install Docker
194+
2. Run make command
181195
```sh
182196
make generate-protos
183197
```

config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
confluent "github.com/confluentinc/confluent-kafka-go/v2/kafka"
1919
githublogrus "github.com/sirupsen/logrus"
2020

21+
"github.com/teslamotors/fleet-telemetry/connector"
2122
"github.com/teslamotors/fleet-telemetry/datastore/googlepubsub"
2223
"github.com/teslamotors/fleet-telemetry/datastore/kafka"
2324
"github.com/teslamotors/fleet-telemetry/datastore/kinesis"
@@ -88,6 +89,12 @@ type Config struct {
8889
// TransmitDecodedRecords if true decodes proto message before dispatching it to supported datastores
8990
TransmitDecodedRecords bool `json:"transmit_decoded_records,omitempty"`
9091

92+
// DataConnector defines sources of supplemental data to enhance the server's functionality
93+
DataConnectorConfig connector.Config `json:"data_connectors,omitempty"`
94+
95+
// DataConnector manages accessing supplemental data from external sources
96+
DataConnector *connector.ConnectorProvider
97+
9198
// MetricCollector collects metrics for the application
9299
MetricCollector metrics.MetricCollector
93100

@@ -235,6 +242,10 @@ func (c *Config) configureMetricsCollector(logger *logrus.Logger) {
235242
c.MetricCollector = metrics.NewCollector(c.Monitoring, logger)
236243
}
237244

245+
func (c *Config) configureDataConnector(logger *logrus.Logger) {
246+
c.DataConnector = connector.NewConnectorProvider(c.DataConnectorConfig, c.MetricCollector, logger)
247+
}
248+
238249
func (c *Config) prometheusEnabled() bool {
239250
if c.Monitoring != nil && c.Monitoring.PrometheusMetricsPort > 0 {
240251
return true

config/config_initializer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func LoadApplicationConfiguration() (config *Config, logger *logrus.Logger, err
3232

3333
config.configureLogger(logger)
3434
config.configureMetricsCollector(logger)
35+
config.configureDataConnector(logger)
3536
return config, logger, nil
3637
}
3738

connector/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Data Connectors
2+
3+
Data connectors can be configured to fetch data from an external source to enhance server functionality.
4+
5+
Currently available capabilities:
6+
- `vin_allowed`: Check if a VIN is allowed to connect to the server.
7+
8+
## Available Connectors
9+
10+
### File
11+
12+
Reads from a JSON file. Watches for file changes at runtime.
13+
14+
- `capabilities`: `[]string` capabilities to use the data connector for.
15+
- `vin_allowed.path`: `string` path of the file to watch.
16+
17+
**Example File**:
18+
```json
19+
{
20+
"vins_allowed": ["VIN1"]
21+
}
22+
```
23+
24+
**Example Config**:
25+
```json
26+
{
27+
"data_connectors": {
28+
"file": {
29+
"capabilities": ["vin_allowed"],
30+
"path": "path/to/file"
31+
}
32+
}
33+
}
34+
```
35+
36+
### Redis
37+
38+
Obtains data from a Redis cache.
39+
40+
- `capabilities`: `[]string` capabilities to use the data connector for.
41+
- `vin_allowed.prefix`: `string` prefix for all Redis keys when checking if a VIN is allowed to connect.
42+
- `vin_allowed.allow_on_failure`: `bool` whether a VIN should be allowed to connect to the server if an error is encountered while fetching from Redis.
43+
44+
**Example Config**:
45+
46+
```json
47+
{
48+
"data_connectors": {
49+
"redis": {
50+
"capabilities": ["vin_allowed"],
51+
"vin_allowed": {
52+
"prefix": "vin_allowed:",
53+
"allow_on_failure": true
54+
}
55+
}
56+
}
57+
}
58+
```
59+
60+
### HTTP
61+
62+
Obtains data from an REST API.
63+
64+
- `capabilities`: `[]string` capabilities to use the data connector for.
65+
- `host`: `string` host of the remote server.
66+
- `timeout_seconds`: `int` seconds to wait for a response.
67+
- `transport`: `http.Transport` [golang transport options](https://pkg.go.dev/net/http#Transport).
68+
- `vin_allowed.cache_results`: `bool` whether results from the API should be cached in memory.
69+
- `vin_allowed.cache_ttl_minutes`: `int` how many minutes each result should be remembered in the cache. Defaults to no expiration.
70+
- `vin_allowed.allow_on_failure`: `bool` whether a VIN should be allowed to connect to the server if an error is encountered while fetching from Redis.
71+
72+
**Example Config**:
73+
74+
```json
75+
{
76+
"data_connectors": {
77+
"http": {
78+
"capabilities": ["vin_allowed"],
79+
"host": "localhost",
80+
"timeout_seconds": 10,
81+
"vin_allowed": {
82+
"cache_results": true,
83+
"cache_ttl_minutes": 60,
84+
"allow_on_failure": false
85+
}
86+
}
87+
}
88+
}
89+
```
90+
91+
### gRPC
92+
93+
Obtains data from a gRPC service.
94+
95+
- `capabilities`: capabilities to use the data connector for.
96+
- `host`: host of the gRPC server.
97+
- `tls.cert_file`: path to cert to use when connecting.
98+
- `tls.key_file`: path to key to use when connecting.
99+
- `vin_allowed.cache_results`: `bool` whether results from the API should be cached in memory.
100+
- `vin_allowed.cache_ttl_minutes`: `int` how many minutes each result should be remembered in the cache. Defaults to no expiration.
101+
- `vin_allowed.allow_on_failure`: `bool` whether a VIN should be allowed to connect to the server if an error is encountered while fetching from Redis.
102+
103+
**Example Config**:
104+
105+
```json
106+
{
107+
"data_connectors": {
108+
"grpc": {
109+
"capabilities": ["vin_allowed"],
110+
"host": "grpc_host",
111+
"tls": {
112+
"cert_file": "path/to/cert",
113+
"key_file": "path/to/key"
114+
},
115+
"vin_allowed": {
116+
"cache_results": true,
117+
"cache_ttl_minutes": 60,
118+
"allow_on_failure": false
119+
}
120+
}
121+
}
122+
}
123+
```

0 commit comments

Comments
 (0)