At the very end a last video I had opened up the tickets indexed not t file and I mentioned that we
still had a year L right here to connect to the incorrect Mongo DB instance.
So this is definitely really bad.
We've got some code inside of here that could have very easily gone on court we might have accidently
deployed this ticket service into production and how to connect to the Ron Mongo DB database.
Definitely not good.
Let's try to figure out some way to solve this and catch issues like this in the future.
Here's one way we could do it very easily.
You might recall that back inside of our infra K AIDS directory.
If we open up our tickets deployment file when we defined as pod spec right here we have the ability
to define some different environment variables.
I think that one easy way to solve this issue potentially in the future would be to add in the URL to
connect you as an environment variable so let's try doing that right now.
We're going to make this change both to our ticket service and to the authentication one as well because
the authentication service is also doing the same thing.
It's got a hardcoded URL right there.
We're going to first start with the ticket stuff so make sure you are inside of the tickets deployment
file and make sure you have open the index dot to file inside of the tickets directory.
Make sure you're not changing the wrong files right now.
OK.
So inside of the tickets deployment file I'm going to find the EMV section we're gonna add in a new
environment variable right here.
I'm gonna add in a new variable with the name of Mongo underscore.
You are I and then I'm going to set its value directly right here.
I'm going to put in value.
I'm going to go back over to my tickets index dot to yes file I'm going to find that you are all right
there I'm going to cut it all then go back over to the deployment file and paste in that string now
the string is still referencing the authentication is Mongo DB for instance.
So let's update that and make sure we are connecting to the tickets one that we just created the deployment
file for a moment ago.
If you need a reminder inside of our tickets Mongo DV or mongo DEP will file.
We can take a look at the name of the service at the bottom.
The name of the service is tickets Mongo SRB.
That is what you're trying to connect to.
So we can find off Mongo SRB and update it two tickets and then remember inside of a mongo DB database
there can technically be multiple different databases.
That's what the name right here at the very end is referring to.
That's the name of the database inside of this database that we're trying to create.
So we're going to update that two tickets not strictly required.
We can have a database name of anything we want but I found it's usually best to just put some consistent
name inside there.
Now one thing what to mentioned is that we have now hardcoded a connection URL inside of a deployment
file.
If we were to commit this file right now to get and then say push it up to get hub anyone in the world
would be able to see this you are all right here.
Is that a problem.
I would really strongly argue no it is not an issue although there is a Your L right here this year
L is only accessible if someone is inside of our cluster.
The only reason I would be concerned about putting my Ural right here as a plain string out online is
if there was an user name and password associated or required to log in or connect to this Mongo DB
instance if there was a username and password required I would definitely not want to read it outright
here as plain text.
But as long as we are just connecting to some playing your l without any security around it.
And as long as we also have this deployment file that very plainly spells out exactly how to connect
to this thing.
I again I'm going to strongly argue and say No it's really not an issue to put this thing right here
as a plane string.
If we did care about that if we had a username and password authentication scheme with Mongo D.B. then
all we'd have to do is define this environment variable inside of a secret just as we did with the Jason
Webb token key.
So that should be it at least for the deployment file.
So I'm going to save the ticket's deployment file and close it I'm now left inside of my tickets indexed
to yes file so inside of here we need to make sure that we try to connect to the Mongo to be instance
with your eye those provided inside that enviroment variable you might recall that before we can access
any environment variable we do have to make sure that actually is defined otherwise typescript is going
to get really unhappy with us.
So I gonna do another very similar check to what we did before I can say if not process EMV Mongo you
are I mean let's throw a new error and I'll say Mongo your eye must be defined and then we can pass
the enviroment variable in as the first argument to Mongoose stock connect the process.
Andy Mongo you're right.
All right so let's say this.
I'm going to go back over to my terminal and I just want to take a look at scaffold and make sure that
everything booted up just fine.
Yep looks like it definitely did.
Now to test this and make sure that we actually are connecting to mongo through that environment variable.
I could go back over to the deployment file or tickets I could find where we just defined that environment
variable and comment that out if I then save this file scaffold should update the deployment that will
cause our pod to be recreated without the environment variable and then we should see an error at our
terminal.
And before I save this and confirm that everything is working as expected I want you to know I have
noticed some really strange inconsistencies with setting environment variables and then changing them
or removing them.
So if I do not immediately see an error I'm not going to be super concerned right away.
We should see an error but if I don't I might be able to solve the issue by just restarting scaffold.
Long story short let's just say this and see what happens.
I'll save the file flip back over I'll see that the deployment was configured.
Then we should see a new pod being created.
I know this is a new pod because it has a different idea than the previous one running our tickets pod
and now in this scenario.
Yeah it looks like it's not quite working as expected here.
I should not have been able to connect to mongo DB.
I should have gotten an error saying that Mongo your eye is not defined one way to try to fix this.
I can go back over to the index dot to yes file and I'm going to try to just hard save this file again
so I can hit command s scaffold is going to think that the file was just updated and it should tried
to sync the file which should cause node Mohn to restart my server.
So I flip back over and now it looks like yeah it did work right here.
So I see that error but also looks like for some reason my server also started backup successfully anyways.
So again I have noticed some weirdness around doing this environment variable checks.
It only seems to be occurring once you start to initially make that change.
But the next time you restart scaffold usually it seems like the issue goes away just to prove that
I'm going to stop scaffold.
Everything gets deleted I'm going to wait.
For everything to actually be terminated
and for some reason it always seems to take all that too long.
There we go.
I'll start it back up.
And now when my tickets service starts back up all this environment variable stuff should be fixed.
So I should see an error start to appear right away.
If it is not defined and there we go there is the bigger Mongo your eye must be defined and I just keep
getting that error an error again because scaffold keeps on trying to restart my pod or to be more precise
the deployment does.
And I would keep them on the same air over and over.
So this is definitely a good improvement for making sure that we capture and just recognize that.
Oh yeah.
We need to update this Mongo D.B. you or I anytime that we start to copy paste this stuff over to another
service.
So last up I'm going to comment that I'm going to save the file.
I'll go back over to my terminal and now I should be back to seeing everything working correctly.
Very good.
All right quick pause right here.
We're then going to go over to our art service and do the exact same thing.
We're going to set up an environment variable.
We're going to reference that environment variable inside of our index not TSA file all inside of.
Off they'll be really quick I promise.
So let's take care of it in just a moment.