I never mentioned 'indefinite immunity'. If you are infected with a virus/bacteria and recover, circulating antibodies produced in response, that are specific to that strain of virus/bacteria, will prevent re-infection. If the virus or bacteria mutate, as influenza viruses do, then new strains are created and cause infection.