1 00:00:00,690 --> 00:00:05,430 At the very end a last video I had opened up the tickets indexed not t file and I mentioned that we 2 00:00:05,430 --> 00:00:10,380 still had a year L right here to connect to the incorrect Mongo DB instance. 3 00:00:10,380 --> 00:00:12,150 So this is definitely really bad. 4 00:00:12,150 --> 00:00:16,650 We've got some code inside of here that could have very easily gone on court we might have accidently 5 00:00:16,650 --> 00:00:21,930 deployed this ticket service into production and how to connect to the Ron Mongo DB database. 6 00:00:21,930 --> 00:00:23,490 Definitely not good. 7 00:00:23,580 --> 00:00:27,600 Let's try to figure out some way to solve this and catch issues like this in the future. 8 00:00:27,600 --> 00:00:29,520 Here's one way we could do it very easily. 9 00:00:29,520 --> 00:00:33,030 You might recall that back inside of our infra K AIDS directory. 10 00:00:33,030 --> 00:00:38,910 If we open up our tickets deployment file when we defined as pod spec right here we have the ability 11 00:00:38,910 --> 00:00:41,290 to define some different environment variables. 12 00:00:41,310 --> 00:00:46,350 I think that one easy way to solve this issue potentially in the future would be to add in the URL to 13 00:00:46,350 --> 00:00:50,900 connect you as an environment variable so let's try doing that right now. 14 00:00:51,010 --> 00:00:56,380 We're going to make this change both to our ticket service and to the authentication one as well because 15 00:00:56,380 --> 00:00:59,140 the authentication service is also doing the same thing. 16 00:00:59,200 --> 00:01:01,970 It's got a hardcoded URL right there. 17 00:01:02,070 --> 00:01:06,300 We're going to first start with the ticket stuff so make sure you are inside of the tickets deployment 18 00:01:06,300 --> 00:01:11,930 file and make sure you have open the index dot to file inside of the tickets directory. 19 00:01:12,000 --> 00:01:15,110 Make sure you're not changing the wrong files right now. 20 00:01:15,860 --> 00:01:16,080 OK. 21 00:01:16,230 --> 00:01:21,920 So inside of the tickets deployment file I'm going to find the EMV section we're gonna add in a new 22 00:01:21,920 --> 00:01:23,500 environment variable right here. 23 00:01:23,650 --> 00:01:27,550 I'm gonna add in a new variable with the name of Mongo underscore. 24 00:01:27,710 --> 00:01:32,730 You are I and then I'm going to set its value directly right here. 25 00:01:32,740 --> 00:01:34,210 I'm going to put in value. 26 00:01:34,470 --> 00:01:39,790 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 27 00:01:39,790 --> 00:01:48,780 there I'm going to cut it all then go back over to the deployment file and paste in that string now 28 00:01:48,780 --> 00:01:53,100 the string is still referencing the authentication is Mongo DB for instance. 29 00:01:53,100 --> 00:01:57,750 So let's update that and make sure we are connecting to the tickets one that we just created the deployment 30 00:01:57,750 --> 00:01:59,330 file for a moment ago. 31 00:01:59,730 --> 00:02:04,370 If you need a reminder inside of our tickets Mongo DV or mongo DEP will file. 32 00:02:04,470 --> 00:02:06,840 We can take a look at the name of the service at the bottom. 33 00:02:06,900 --> 00:02:09,390 The name of the service is tickets Mongo SRB. 34 00:02:09,510 --> 00:02:11,380 That is what you're trying to connect to. 35 00:02:11,730 --> 00:02:19,130 So we can find off Mongo SRB and update it two tickets and then remember inside of a mongo DB database 36 00:02:19,160 --> 00:02:22,470 there can technically be multiple different databases. 37 00:02:22,490 --> 00:02:25,460 That's what the name right here at the very end is referring to. 38 00:02:25,460 --> 00:02:29,370 That's the name of the database inside of this database that we're trying to create. 39 00:02:29,420 --> 00:02:32,630 So we're going to update that two tickets not strictly required. 40 00:02:32,630 --> 00:02:37,280 We can have a database name of anything we want but I found it's usually best to just put some consistent 41 00:02:37,280 --> 00:02:38,220 name inside there. 42 00:02:39,560 --> 00:02:43,950 Now one thing what to mentioned is that we have now hardcoded a connection URL inside of a deployment 43 00:02:43,950 --> 00:02:44,760 file. 44 00:02:44,760 --> 00:02:50,310 If we were to commit this file right now to get and then say push it up to get hub anyone in the world 45 00:02:50,340 --> 00:02:52,670 would be able to see this you are all right here. 46 00:02:52,710 --> 00:02:54,170 Is that a problem. 47 00:02:54,210 --> 00:03:00,030 I would really strongly argue no it is not an issue although there is a Your L right here this year 48 00:03:00,030 --> 00:03:03,500 L is only accessible if someone is inside of our cluster. 49 00:03:03,510 --> 00:03:08,370 The only reason I would be concerned about putting my Ural right here as a plain string out online is 50 00:03:08,370 --> 00:03:14,360 if there was an user name and password associated or required to log in or connect to this Mongo DB 51 00:03:14,430 --> 00:03:20,700 instance if there was a username and password required I would definitely not want to read it outright 52 00:03:20,700 --> 00:03:22,050 here as plain text. 53 00:03:22,380 --> 00:03:26,910 But as long as we are just connecting to some playing your l without any security around it. 54 00:03:27,030 --> 00:03:32,820 And as long as we also have this deployment file that very plainly spells out exactly how to connect 55 00:03:32,820 --> 00:03:33,750 to this thing. 56 00:03:33,870 --> 00:03:38,550 I again I'm going to strongly argue and say No it's really not an issue to put this thing right here 57 00:03:38,580 --> 00:03:39,840 as a plane string. 58 00:03:40,200 --> 00:03:45,600 If we did care about that if we had a username and password authentication scheme with Mongo D.B. then 59 00:03:45,630 --> 00:03:51,150 all we'd have to do is define this environment variable inside of a secret just as we did with the Jason 60 00:03:51,150 --> 00:03:54,550 Webb token key. 61 00:03:54,640 --> 00:03:57,480 So that should be it at least for the deployment file. 62 00:03:57,550 --> 00:04:04,270 So I'm going to save the ticket's deployment file and close it I'm now left inside of my tickets indexed 63 00:04:04,310 --> 00:04:09,890 to yes file so inside of here we need to make sure that we try to connect to the Mongo to be instance 64 00:04:10,220 --> 00:04:15,400 with your eye those provided inside that enviroment variable you might recall that before we can access 65 00:04:15,430 --> 00:04:19,850 any environment variable we do have to make sure that actually is defined otherwise typescript is going 66 00:04:19,850 --> 00:04:21,130 to get really unhappy with us. 67 00:04:21,690 --> 00:04:29,500 So I gonna do another very similar check to what we did before I can say if not process EMV Mongo you 68 00:04:29,500 --> 00:04:39,850 are I mean let's throw a new error and I'll say Mongo your eye must be defined and then we can pass 69 00:04:39,850 --> 00:04:44,460 the enviroment variable in as the first argument to Mongoose stock connect the process. 70 00:04:44,520 --> 00:04:47,740 Andy Mongo you're right. 71 00:04:50,970 --> 00:04:55,390 All right so let's say this. 72 00:04:55,600 --> 00:04:59,720 I'm going to go back over to my terminal and I just want to take a look at scaffold and make sure that 73 00:04:59,720 --> 00:05:01,410 everything booted up just fine. 74 00:05:01,460 --> 00:05:03,540 Yep looks like it definitely did. 75 00:05:03,560 --> 00:05:08,670 Now to test this and make sure that we actually are connecting to mongo through that environment variable. 76 00:05:08,720 --> 00:05:14,480 I could go back over to the deployment file or tickets I could find where we just defined that environment 77 00:05:14,480 --> 00:05:20,960 variable and comment that out if I then save this file scaffold should update the deployment that will 78 00:05:20,960 --> 00:05:26,180 cause our pod to be recreated without the environment variable and then we should see an error at our 79 00:05:26,180 --> 00:05:27,140 terminal. 80 00:05:27,140 --> 00:05:32,470 And before I save this and confirm that everything is working as expected I want you to know I have 81 00:05:32,520 --> 00:05:38,090 noticed some really strange inconsistencies with setting environment variables and then changing them 82 00:05:38,120 --> 00:05:39,420 or removing them. 83 00:05:39,440 --> 00:05:44,130 So if I do not immediately see an error I'm not going to be super concerned right away. 84 00:05:44,210 --> 00:05:49,830 We should see an error but if I don't I might be able to solve the issue by just restarting scaffold. 85 00:05:49,850 --> 00:05:52,450 Long story short let's just say this and see what happens. 86 00:05:52,530 --> 00:05:56,860 I'll save the file flip back over I'll see that the deployment was configured. 87 00:05:57,200 --> 00:05:59,390 Then we should see a new pod being created. 88 00:05:59,390 --> 00:06:03,590 I know this is a new pod because it has a different idea than the previous one running our tickets pod 89 00:06:04,120 --> 00:06:05,300 and now in this scenario. 90 00:06:05,300 --> 00:06:08,810 Yeah it looks like it's not quite working as expected here. 91 00:06:08,840 --> 00:06:11,260 I should not have been able to connect to mongo DB. 92 00:06:11,290 --> 00:06:16,940 I should have gotten an error saying that Mongo your eye is not defined one way to try to fix this. 93 00:06:16,940 --> 00:06:21,530 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 94 00:06:21,560 --> 00:06:26,950 so I can hit command s scaffold is going to think that the file was just updated and it should tried 95 00:06:26,950 --> 00:06:31,000 to sync the file which should cause node Mohn to restart my server. 96 00:06:31,190 --> 00:06:35,240 So I flip back over and now it looks like yeah it did work right here. 97 00:06:35,320 --> 00:06:41,920 So I see that error but also looks like for some reason my server also started backup successfully anyways. 98 00:06:42,100 --> 00:06:47,300 So again I have noticed some weirdness around doing this environment variable checks. 99 00:06:47,440 --> 00:06:50,910 It only seems to be occurring once you start to initially make that change. 100 00:06:51,010 --> 00:06:55,240 But the next time you restart scaffold usually it seems like the issue goes away just to prove that 101 00:06:55,270 --> 00:06:57,340 I'm going to stop scaffold. 102 00:06:57,340 --> 00:07:00,380 Everything gets deleted I'm going to wait. 103 00:07:01,170 --> 00:07:02,560 For everything to actually be terminated 104 00:07:06,190 --> 00:07:09,030 and for some reason it always seems to take all that too long. 105 00:07:09,030 --> 00:07:10,150 There we go. 106 00:07:10,180 --> 00:07:11,110 I'll start it back up. 107 00:07:14,410 --> 00:07:19,390 And now when my tickets service starts back up all this environment variable stuff should be fixed. 108 00:07:19,460 --> 00:07:21,680 So I should see an error start to appear right away. 109 00:07:21,700 --> 00:07:28,900 If it is not defined and there we go there is the bigger Mongo your eye must be defined and I just keep 110 00:07:28,900 --> 00:07:34,710 getting that error an error again because scaffold keeps on trying to restart my pod or to be more precise 111 00:07:34,720 --> 00:07:35,550 the deployment does. 112 00:07:35,650 --> 00:07:38,650 And I would keep them on the same air over and over. 113 00:07:38,810 --> 00:07:43,550 So this is definitely a good improvement for making sure that we capture and just recognize that. 114 00:07:43,760 --> 00:07:44,020 Oh yeah. 115 00:07:44,030 --> 00:07:49,070 We need to update this Mongo D.B. you or I anytime that we start to copy paste this stuff over to another 116 00:07:49,070 --> 00:07:49,600 service. 117 00:07:49,940 --> 00:07:53,310 So last up I'm going to comment that I'm going to save the file. 118 00:07:53,480 --> 00:07:58,030 I'll go back over to my terminal and now I should be back to seeing everything working correctly. 119 00:07:58,050 --> 00:07:59,780 Very good. 120 00:07:59,860 --> 00:08:01,030 All right quick pause right here. 121 00:08:01,030 --> 00:08:04,140 We're then going to go over to our art service and do the exact same thing. 122 00:08:04,240 --> 00:08:06,400 We're going to set up an environment variable. 123 00:08:06,580 --> 00:08:10,030 We're going to reference that environment variable inside of our index not TSA file all inside of. 124 00:08:10,030 --> 00:08:12,190 Off they'll be really quick I promise. 125 00:08:12,190 --> 00:08:13,720 So let's take care of it in just a moment.